Professional Documents
Culture Documents
(cont)
.data
• We want to write a program that adds the following three bytes: myBytes BYTE 80h,66h,0A5h
.data
myBytes BYTE 80h,66h,0A5h
• How about the following code. Is anything missing?
• What is your evaluation of the following code?
movzx ax,myBytes
mov al,myBytes
mov bl,[myBytes+1]
add al,[myBytes+1]
add ax,bx
add al,[myBytes+2]
mov bl,[myBytes+2]
add ax,bx ; AX = sum
• What is your evaluation of the following code?
mov ax,myBytes
add ax,[myBytes+1]
add ax,[myBytes+2]
Yes: Move zero to BX before the MOVZX instruction.
• Any other possibilities?
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 16 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 17
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 18 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 19
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 20 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 21
1
INC and DEC Instructions INC and DEC Examples
.data
• Add 1, subtract 1 from destination operand myWord WORD 1000h
• operand may be register or memory myDword DWORD 10000000h
• INC destination .code
inc myWord ; 1001h
• Logic: destination destination + 1 dec myWord ; 1000h
• DEC destination inc myDword ; 10000001h
• Logic: destination destination – 1
mov ax,00FFh
inc ax ; AX = 0100h
mov ax,00FFh
inc al ; AX = 0000h
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 22 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 23
Write a short program demonstrating that the INC and DEC instructions do
Your turn... not affect the Carry flag.
.code
main PROC
Show the value of the destination operand after each of the
following instructions executes: ; INC does not affect the carry flag:
mov al,254
add al,1 ; AL=255, CF=0
.data
call DumpRegs
myByte BYTE 0FFh, 0
inc al ; AL=0, CF=0 (unchanged)
.code
call DumpRegs
mov al,myByte ; AL = FFh
mov ah,[myByte+1] ; AH = 00h
dec ah ; AH = FFh ; DEC does not affect the carry flag:
mov al,1
inc al ; AL = 00h
sub al,1 ; AL=0, CF=0
dec ax ; AX = FEFF
call DumpRegs
dec al ; AL=255, CF=0 (unchanged)
call DumpRegs
exit
main ENDP
END main
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 24 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 25
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 26 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 27
2
INCLUDE Irvine32.inc
Description: Write a program that uses addition and .data
.code
subtraction to set and clear the Carry flag. After each main PROC
instruction, insert the call DumpRegs statement to
display the registers and flags. Using comments, explain ;adding 1 to 255 rolls AL over to zero:
mov al,255
how (and why) the Carry flag was affected by each add al,1 ; AL=0, CF=1 (unsigned overflow)
instruction. call DumpRegs
exit
main ENDP
28
END main 29
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
Write a program that uses addition and subtraction to set and clear the overflow
flag. After each addition or subtraction instruction, insert the call DumpRegs NEG (negate) Instruction
statement to display the registers and flags. Using comments, explain how (and
why) the Overflow flag was affected by each instruction. Optional: include an
ADD instruction that sets both the Carry and Overflow flags. Reverses the sign of an operand. Operand can be a register or
memory operand.
mov al,+127 ; AL=7Fh
add al,1 ; AL=80h, OF=1
call DumpRegs .data
valB BYTE -1
sub al,1 ; AL=7Fh, OF=1 valW WORD +32767
call DumpRegs .code
mov al,valB ; AL = -1
sub al,1 ; AL=7Eh, OF=0
call DumpRegs
neg al ; AL = +1
neg valW ; valW = -32767
mov al,-128
add al,-1
call DumpRegs ; AL=7Fh, OF=1
Suppose AX contains –32,768 and we apply NEG to it. Will
; Optional: the result be valid?
mov al,80h
add al,80h ; AL=0, CF=1, OF=1
call DumpRegs
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 30 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 31
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 32 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 33
3
Your turn... Flags Affected by Arithmetic
Translate the following expression into assembly language. Do
not permit Xval, Yval, or Zval to be modified: • The ALU has a number of status flags that reflect the
Rval = Xval - (-Yval + Zval)
outcome of arithmetic (and bitwise) operations
• based on the contents of the destination operand
Assume that all values are signed doublewords. • Essential flags:
mov ebx,Yval • Zero flag – set when destination equals zero
neg ebx • Sign flag – set when destination is negative
add ebx,Zval
mov eax,Xval
• Carry flag – set when unsigned value is out of range
sub eax,ebx • Overflow flag – set when signed value is out of range
• The MOV instruction never affects the flags.
mov Rval,eax
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 34 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 35
affect
status flags
branching logic
Remember...
• A flag is set when it equals 1.
You can use diagrams such as these to express the relationships between assembly • A flag is clear when it equals 0.
language concepts.
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 36 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 37
4
Overflow and Carry Flags Carry Flag (CF)
A Hardware Viewpoint
The Carry flag is set when the result of an operation generates an
• How the ADD instruction modifies OF and CF: unsigned value that is out of range (too big or too small for the
• OF = (carry out of the MSB) XOR (carry into the MSB) destination operand).
• CF = (carry out of the MSB)
mov al,0FFh
add al,1 ; CF = 1, AL = 00
• How the SUB instruction modifies OF and CF:
; Try to go below zero:
• NEG the source and ADD it to the destination
• OF = (carry out of the MSB) XOR (carry into the MSB) mov al,0
sub al,1 ; CF = 1, AL = FF
• CF = INVERT (carry out of the MSB)
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 42 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 43
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 44 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 45
5
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 46 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 47
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 48 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 49