0% found this document useful (0 votes)
10 views19 pages

Tuto 3

This document provides a tutorial on assembly language programming for the Intel x86 architecture. It recaps flag registers, explains the programming model including registers and memory addressing, covers data addressing modes, and discusses repetition and looping instructions like REP, LOOP, and conditional jumps.

Uploaded by

zelalem2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Tuto 3

This document provides a tutorial on assembly language programming for the Intel x86 architecture. It recaps flag registers, explains the programming model including registers and memory addressing, covers data addressing modes, and discusses repetition and looping instructions like REP, LOOP, and conditional jumps.

Uploaded by

zelalem2022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

ELEG3230B Tutorial 3

Janice Law
SHB 729
Outline
„ Recap: Flag Registers
„ Programming Model
„ Data Addressing Mode
„ Assembly Language & Instruction Set (I)
‰ REP, REPZ, REPNZ
‰ CMPS, SCAS
‰ LOOPING
„ Summary

04/10/2010 ELEG3230B Tutorial 3 2


Recap – Flag registers

„ 6 condition flags in flag register


‰ CF (carry flag): set to 1 when the result of an addition
has a carry out of MSB.
‰ PF (parity flag): set to 1 if the result has an even
number of 1s
‰ AF (auxiliary carry flag): set to 1 if there is a carry/
borrow of bit-3
‰ ZF (zero flag): set to 1 if the result is zero
‰ SF (sign flag): equal to MSB of result
‰ OF (overflow flag): set to 1 if the result is out of range

04/10/2010 ELEG3230B Tutorial 3 3


Recap – Flag registers

„ What are the result values in Carry, Zero, Sign,


Parity, Auxiliary Carry, and Overflow flags?
‰ 0110 1001 + 0001 1001

‰ 0111 1111 + 1000 0001

‰ 0101 1011 + 1101 0010

‰ 45E9h + 546Ah

04/10/2010 ELEG3230B Tutorial 3 4


Programming Model

04/10/2010 ELEG3230B Tutorial 3 5


Programming Model – Registers

„ General Registers
‰ General use (like variables)
‰ Special purpose
‰ AX: stores the result of arithmetic and logic
instructions
‰ BX: stores the base (offset) address in XLAT
instruction
‰ CX: stores the loop count for instruction

‰ DX: stores the most significant part of the result in


16-bit multiplication

04/10/2010 ELEG3230B Tutorial 3 6


Programming Model – Registers

„ Pointer and Index Registers


‰ SP, BP, SI, DI usually store offset addresses
‰ SP: Stack Pointer

‰ BP: Base Pointer

‰ DI: Destination Index

‰ SI: Source Index

‰ IP usually stores offset address of the next instruction

04/10/2010 ELEG3230B Tutorial 3 7


Programming Model – Registers
„ Segment Registers
‰ Stores the initial address information of the
corresponding memory segments
‰ CS: Code Segment

‰ DS: Data Segment

‰ SS: Stack Segment

‰ ES: Extra Segment

04/10/2010 ELEG3230B Tutorial 3 8


Programming Model – Segment and Offset

„ 20-bit address in memory:


16-bit segment address + 16-bit offset

„ Calculate the physical address with


CS = 348Ah and IP = 4214h.

04/10/2010 ELEG3230B Tutorial 3 9


Programming Model – Segment and Offset

„ If it doesn’t state the segment, always use the


default segment register!
„ Examples:
‰ MOV CL, [BP] (SS:[BP] -> CL)
‰ MOV CL, DS:[BP] (DS:[BP] -> CL)

04/10/2010 ELEG3230B Tutorial 3 10


Programming Model – Sample Problems

„ Assume
ES=6000h, CS=4000h, SS=7000h, DS=5000h,
IP=43E8h, SP=0000h, BP=9468h, SI=4C00h,
DI=7D00h, AX=4235h, BX=075Ah, CX=0004h,
DX=3302h.
‰ What is the next instruction’s physical address?
‰ What is the physical address of the top of the stack?
‰ Show the result of MOV ES:[BP], AH. Give the physical
address if a memory is affected.

04/10/2010 ELEG3230B Tutorial 3 11


Data Addressing Mode

„ Register: move from registers (AX, BX, AL,…)


„ Immediate: move a constant value (without a [ ])
„ Direct: move a content of an address (value with a [ ])
„ Register Indirect: move a content of an address stored in a
register
„ Based: move using base pointers (BP/BX)
„ Indexed: move using index pointers (DI,SI)
„ Based Indexed: move using both base and index pointers
(e.g., [BP+SI])
„ String: move DS:[SI] to ES:[DI]
„ Port: Communicate to I/O ports (using IN/ OUT)

04/10/2010 ELEG3230B Tutorial 3 12


Data Addressing Mode (Con’t)

04/10/2010 ELEG3230B Tutorial 3 13


Data Addressing Mode – Sample Problems

„ State the physical address and content (if


available) of the destination for each of the
following problems. Assume
DS=3000h, ES=4000h, SS=5000h, CS=6000h,
IP=3388h, SP=0FFh, BP=2468h, SI=4000h,
DI=50DDh, AX=2345h, BX=77EEh, CX=3333h,
DX=2222h.
‰ MOV CL, 12h
‰ MOV [BP], DX
‰ MOVSB
‰ MOV WORD PTR [SI+2], 20h

04/10/2010 ELEG3230B Tutorial 3 14


Assembly Language & Instruction Set (I)
„ REP, REPZ, REPNZ
‰ Repeats an instruction and decrements the count in CX
‰ REP: repeats if CX is nonzero
‰ REPZ: repeats if CX is nonzero and zero flag is 1
‰ REPNZ: repeats if CX is nonzero and zero flag is 0

„ Example
MOV AX, 0h
MOV BX, 1h
MOV CX, 0h
REP ADD AX, BX
What is the value of AX and BX?

04/10/2010 ELEG3230B Tutorial 3 15


Assembly Language & Instruction Set (I)
„ CMPS (compare string) & SCAS (scan string)
„ REPZ CMPS stringA, stringB
‰ Set CX to the length of the string, compare stringA with
stringB
stringA: 123456123456
stringB: 123486123456
ZF of CMP: 0 0 0 0 1 <- stop!

„ REPNZ SCAS stringA


‰ Scan stringA until item in accumulator is found
stringA: 123456123456
AL : 5 5 5 5 5 <- stop!

04/10/2010 ELEG3230B Tutorial 3 16


Assembly Language & Instruction Set (I)

„ LOOPING
‰ JNZ : Conditional jump, jump not zero; if Z=0

„ Example
MOV CX, 0Ah
MOV AX, 5h
LOOOP: DEC CX, 1h
AND AX, CX
JNZ LOOOP
What are the values of AX and CX?

04/10/2010 ELEG3230B Tutorial 3 17


Assembly Language & Instruction Set (I)

„ LOOPING
‰ LOOPNZ : Jump if both CX and Z are nonzero

„ Example
MOV CX, 0Ah
MOV AX, 5h
LOOOP: AND AX, CX
LOOPNZ LOOOP
What are the values of AX and CX?

04/10/2010 ELEG3230B Tutorial 3 18


Summary
„ Recap: Flag registers
‰ CF, PF, AF, ZF, SF, OF
„ Programming Model
‰ General registers, pointer and index registers,
segment registers
‰ Calculate physical address with segment and offset
„ Data Addressing Mode
‰ Register, Immediate, Direct, Register Indirect, Based,
Indexed, Based Indexed, String, Port
„ Assembly Language & Instruction Set (I)
‰ REP, REPZ, REPNZ, CMPS, SCAS, LOOPING

04/10/2010 ELEG3230B Tutorial 3 19

You might also like