- Faculty of Computing

advertisement
Department of Computer Systems and Communication
Faculty of Computer Science and Information Systems
UNIVERSITI TEKNOLOGI MALAYSIA
SUBJECT NAME:
COMPUTER ORGANIZATION AND ARCHITECTURE
SUBJECT CODE:
SCR 1043
SEMESTER:
2/2009/2010
LAB TITLE:
LAB 3:
Programming 3: Flags, OFFSET, Arrays, JMP, LOOP
INSTRUCTION:
Student is required to fill the answer in the space provided at
each of lab exercises given.
STUDENT INFO :
Name:_______________________________________
Metric No: ___________________________________
Section: _____________________________________
Email: ______________________________________
SUBMITTED DATE:
_________________
COMMENTS:
MARKS:
________
6
1
Programming 3: Flags, OFFSET, Arrays, JMP, LOOP
Flags Affected by Arithmetic
 The ALU has a number of status flags that reflect the outcome of
arithmetic (and bitwise) operations
o based on the contents of the destination operand
 Essential flags:
o Zero flag – set when destination equals zero
o Sign flag – set when destination is negative
o Carry flag – set when unsigned value is out of range
o Overflow flag – set when signed value is out of range
 The MOV instruction never affects the flags.
Zero Flag (ZF)
 The Zero flag is set when the result of an operation produces zero in the
destination operand.
o mov cx,1
o sub cx,1
; CX = 0, ZF = 1
o mov ax,0FFFFh
o inc ax ; AX = 0, ZF = 1
o inc ax ; AX = 1, ZF = 0
Sign Flag (SF)
 The Sign flag is set when the destination operand is negative. The flag is
clear when the destination is positive.
o mov cx,0
o sub cx,1
; CX = -1, SF = 1
o add cx,2
; CX = 1, SF = 0
 The sign flag is a copy of the destination's highest bit:
o mov al,0
o sub al,1
; AL = 11111111b, SF = 1
o add al,2
; AL = 00000001b, SF = 0
Overflow and Carry Flags: A Hardware Viewpoint
 How the ADD instruction modifies OF and CF:
o OF = (carry out of the MSB) XOR (carry into the MSB)
o CF = (carry out of the MSB)
 How the SUB instruction modifies OF and CF:
o NEG the source and ADD it to the destination
o OF = (carry out of the MSB) XOR (carry into the MSB)
o CF = INVERT (carry out of the MSB)
MSB = Most Significant Bit (high-order bit)
XOR = eXclusive-OR operation
NEG = Negate (same as SUB 0,operand)
2
Carry Flag (CF)
 The Carry flag is set when the result of an operation generates an
unsigned value that is out of range (too big or too small for the
destination operand).
o mov al,0FFh
o add al,1 ; CF = 1, AL = 00
o ; Try to go below zero:
o mov al,0
o sub al,1
; CF = 1, AL = FF
Overflow Flag (OF)
 The Overflow flag is set when the signed result of an operation is
invalid or out of range.
o ; Example 1
o mov al,+127
o add al,1
; OF = 1, AL = ??
o
o ; Example 2
o mov al,7Fh ; OF = 1, AL = 80h
o add al,1
 The two examples are identical at the binary level because 7Fh equals
+127. To determine the value of the destination operand, it is often
easier to calculate in hexadecimal.
OFFSET Operator
 OFFSET returns the distance in bytes, of a label from the beginning of
its enclosing segment
o Protected mode: 32 bits
o Real mode: 16 bits
offset
Data segment:
myByte
 Example:
o Let's assume that the data segment begins at 00404000h:
3
.data
bVal BYTE ?
wVal WORD ?
dVal DWORD ?
dVal2 DWORD ?
.code
mov esi,OFFSET
mov esi,OFFSET
mov esi,OFFSET
mov esi,OFFSET
bVal
wVal
dVal
dVal2
;
;
;
;
ESI
ESI
ESI
ESI
=
=
=
=
00404000
00404001
00404003
00404007
Arrays
 How to handle an array? Use register as a pointer, the method is called
indirect addressing.
 Array Sum Example:
o Indirect operands are ideal for traversing an array. Note that the
register in brackets must be incremented by a value that matches
the array type.
.data
arrayW WORD 1000h,2000h,3000h
.code
mov esi,OFFSET arrayW
mov ax,[esi]
add esi,2
; or: add esi,TYPE arrayW
add ax,[esi]
add esi,2
add ax,[esi] ; AX = sum of the array
JMP (Jump)
 Causes unconditional transfer to a destination (label) that is usually
within the same procedure.
 Format: JMP destination
 Logic: EIP  target
 Examples:
o top: .
.
.
jmp top
; repeat the endless loop
4
LOOP
 repeats a block of statements a specific number of times. ECX is
automatically used as a counter and is decremented each time the loops
repeats.
 Format: LOOP destination
 Logic:
o ECX  ECX – 1
o if ECX != 0, jump to destination
 Implementation:
o The assembler calculates the distance, in bytes, between the
offset of the following instruction and the offset of the target
label. It is called the relative offset.
o The relative offset is added to EIP.
 The following loop calculates the sum of the integers 5 + 4 + 3 +2 + 1:
offset
00000000
00000004
machine code
66 B8 0000
B9 00000005
00000009
0000000C
0000000E
66 03 C1
E2 FB
source code
mov ax,0
mov ecx,5
L1: add ax,cx
loop L1
 When LOOP is assembled, the current location = 0000000E (offset of
the next instruction). –5 (FBh) is added to the current location, causing
a jump to location 00000009:
00000009  0000000E + FB
Exercises Lab:
1. For each of the following marked entries, show the values of the
destination operand and the Sign, Zero, and Carry flags:
mov ax,00FFh
add ax,1
; AX= _____________________ SF= ___ ZF= ___ CF= ___
sub ax,1
; AX= _____________________ SF= ___ ZF= ___ CF= ___
add al,1
; AL= _____________________ SF= ___ ZF= ___ CF= ___
mov bh,6Ch
add bh,95h
; BH= _____________________ SF= ___ ZF= ___ CF= ___
mov al,2
sub al,3
; AL= _____________________ SF= ___ ZF= ___ CF= ___
5
2. What will be the values of the Overflow flag?
mov al,80h
add al,92h ; AL= _____________________ OF = _____
mov al,-2
add al,+127 ; AL= _____________________ OF = _____
3. What will be the values of the given flags after each operation?
mov al,-128
neg al
; AL= ___________________ CF = ____ OF =____
mov ax,8000h
add ax,2
; AX= ___________________ CF = ____ OF =____
mov ax,0
sub ax,2
; AX= ___________________ CF = ____ OF =____
mov al,-5
sub al,+125 ; AL= ___________________ CF = ____ OF =____
4. What will be the final value of AX?
mov ax,20
mov ecx,4
L1:
inc ax
neg ax
loop L1
In each Loop:
Loop 1: AX= ___________________
Loop 2: AX= ___________________
Loop 3: AX= ___________________
Loop 4: AX= ___________________
6
5. The following code calculates the sum of an array of 16-bit integers.
.data
intArray WORD 100h,200h,300h,400h
.code
mov edi,OFFSET intArray
; address of intArray EDI= ______________
mov ecx,LENGTHOF intArray
mov ax,0
; loop counter ECX= ______________
; zero the accumulator
L1:
add ax,[edi]
; add an integer
add edi,TYPE intArray
loop L1
; point to next integer
; repeat until ECX = 0
AX= _____________________,
Number of Loop: = ______________
What changes would you make to the program if you were summing a
doubleword array?
.data
intArray ___________ 100h,200h,300h,400h
.code
mov edi,OFFSET intArray
; address of intArray EDI= ______________
mov ecx,LENGTHOF intArray
mov _______, 0
; loop counter ECX= ______________
; zero the accumulator (use accumulator AX)
L1:
add _______,[edi]
; add an integer
add edi,TYPE intArray
loop L1
; point to next integer
; repeat until ECX = 0
EAX= _____________________,
Number of Loop: = _____________
7
Download