CENG 222

advertisement
CENG 222 – Computer Organization
Lab Work 3
Special Register: FLAGS
The flags register is unlike the other registers on the microprocessor. The other registers hold
eight or 16 bit values. The flags register is simply a collection of one bit values which help
determine the current state of the processor. Although the flags register is 16 bits wide, some
of them are used. Of these flags, four flags you use all the time: zero, carry, sign, and
overflow. These flags are the 8086 condition codes. The flags register appears below:
Zero flag bit is set when last instruction produces 0 as a result.
Carry flag bit is set when last instruction produces a number with a carry.
Sign flag bit is set when last instruction produces a negative number.
Overflow flag bit is set when last instruction produces a number which cannot fit in register.
Flag bits are generally used for testing the conditions. For example, if you want to test
whether A = B, you need to subtract A from B, then if you have a result 0, zero flag will be
set. Therefore, you need to check zero flag to obtain the result of the condition.
Arithmetic operations
Some of the arithmetic operations that are supported by Intel microprocessors are:
ADD, ADC, INC, DEC, SUB, MUL, IMUL, DIV, IDIV
ADD <destination>, <addend>
; destination = destination + addend
ADD instruction adds <addend> to <destination>.
ADD ax, bx
ADD var, 2
ADD al, cl
; ax = ax + bx
; var = var + 2
; al = al + cl
ADC <destination>, <addend>
; destination = destination + addend + C
ADC instruction adds <addend> + Carry to <destination>. It is used to add large sized
numbers.
Let AX:BX be 32 bits number. Let CX:DX be another 32 bits number. In order to add these
numbers together using 16-bits microprocessor, we need to use ADC.
ADD BX, DX
ADC AX, CX
; Add low order digits
; Add high order digits together with carry
The instructions above simply calculates: (AX:BX) = (AX:BX) + (CX:DX)
INC <destination>
DEC <destination>
; destination = destination + 1;
; destination = destination - 1;
INC instruction increments and DEC instruction decrements the destination.
INC dx
DEX al
INC var
SUB <destination>, <subtrahend>
; destination = destination – subtrahend
SUB instruction subtracts <subtrahend> from <destination>.
SUB var, 2
SUB bl, cl
SUB bx, ax
; var = var - 2;
; bl = bl – cl;
; bx = bx – ax;
MUL <multiplicand>
MUL instruction multiplies AX or AL register by <multiplicand>. Destination is AX and DX.
If <multiplicand> is an 8-bits number, it will be multiplied by AL. The destination is AX. If
<multiplicand> is a 16-bits number, it will be multiplied by AX. The destination is (DX:AX).
Here are some examples to make it clear:
MUL DL
MUL BX
MUL var
MUL var
; AX = AL * DL
; (DX:AX) = AX * BX
; AX = AL * var
(Assuming var is a byte)
; (DX:AX) = AX * var
(Assuming var is a word)
IMUL is the same as MUL, but it is used for signed numbers which are stored in 2’s
complement format.
DIV <divisor>
DIV instruction divides AX or (DX:AX) by <divisor>.
If <divisor> is an 8-bits number,
AL = AX / <divisor>
AH = AX mod <divisor>
If <divisor> is a 16-bits number,
AX = (DX:AX) / <divisor>
DX = (DX:AX) mod <divisor>
Here is piece of code for division.
MOV AX, 1000
MOV DL, 30
DIV DL
The code above divides 1000 by 30. The quotient (33) will be in AL. The remainder (10) will
be in AH. If the number is in <divisor> is too small or zero, the quotient will be too large to
fit in AL. Therefore you need to select the registers according to their sizes. For example if
you want to divide 1000 by 3, you will get 333 as a quotient which cannot be stored in 8-bits
register AL! In order to do this, you need to use 16-bits division. Here is how:
MOV AX, 1000
MOV DX, 0
MOV BX, 3
DIV BX
; The number is 1000.
; DX = 0, so (DX:AX) is 0000:1000 = 1000.
; Divisor is 3
; AX = (DX:AX) / 3 = 1000 / 3 = 333. AX is capable to store 333.
; DX = (DX:AX) mod 3 = 1000 mod 3 = 1.
IDIV is the same as DIV, but it is used for signed numbers which are stored in 2’s
complement format.
Jump and Labels
JMP instruction allows to “jump” into an address of a instruction in our program. You can
make loops by using JMP. Usage of JMP:
JMP <address or label name>
In assembly programs, we generally use labels to mark a starting point of a code. They end
with a colon sign (“:”). Example:
L1:
…
MOV …
SUB …
…
JMP L1
; Jump into the first instruction after the label “L1”.
Testing conditions and Conditional Jump
In order to test a condition, you can use SUB or AND instructions. For example you can test
A = B, A > B, A >= B, A < B and A <=B conditions using SUB. You can also test the status
of any bit of a register by using AND. These instructions will modify the flag bits according
to the results they produce. Therefore, you can make a conditional jump using flag bits’
status. However, SUB and AND instructions also store the result into the destination register.
If you just want to test the condition without modifying operands, you need to use CMP and
TEST instructions. These instructions are used to modify just the flag bits.
CMP is the same as SUB but it does not modify the destination. TEST is the same as AND
but it does not modify the destination. However they both modify the flag bits.
Analyze the code below:
…
MOV CX, 0
L1:
CMP CX, 10
JE L2
INC CX
JMP L1
; Go to L2 if CX = 10
; Go to L1 (CMP instruction)
L2:
…
Some of conditional jump instructions are:
JE, JNE, JZ, JNZ, JG, JNG, JL, JNL, JLE, JGE, JNLE, JNGE
JE:
Jump if equal to
JNE: Jump if not equal to
JZ:
Jump if zero
JNZ: Jump if not zero
JG: Jump if greater than
JNG: Jump if not greater than
JL:
Jump if less than
JNL: Jump if not less than
JLE: Jump if less than or equal to
JGE: Jump if greater than or equal to
JNLE: Jump if not less than nor equal to (same as JG)
JNGE:Jump if not greater than nor equal to (same as JL)
Please note that conditional jump instructions are useless alone, but if you use an instruction
that modifies flag bits, before the jump instruction, you can make IF…THEN…ELSE
structures. For example:
CMP var1, var2
JG L1
; Compare var1 and var2
; Jump to L1 if var1>var2
…
…
; “IF” block
; These instructions will be executed if var1<=var2
JMP L2
; End of “IF” block, we need to jump L2
…
…
; “ELSE” block
; These instructions will be executed if var1>var2
L1:
L2:
; End of “IF…ELSE” block
LOOP instruction
LOOP instruction allows creating a simple loop using CX register as a counter.
Usage:
LOOP <address of label>
You need to initialize CX first for the number of repentance of the loop. Analyze the code
below:
MOV CX, 10
; Loop 10 times
…
…
…
LOOP L1
; Instructions in the loop
L1:
LOOP instruction simply decrements CX and tests if it is zero. If not zero, then jump.
Otherwise, continue.
CALL and RET instructions
CALL instruction is used for calling a subroutine/procedure/function.
Usage:
CALL <address or procedure name>
RET instruction is used for returning back to caller procedure from the called procedure. RET
has no arguments. Analyze the code given in experiment.
Using stack
Stack is used for temporarily storing data to memory. It is also used by CALL and RET to
store caller instruction address. Hence, it is critically important to use stack properly:
Leave stack as how you find!
It means that you need to get all the data out which you store in the stack. Do not forget any
data in the stack. You can use it temporarily!
PUSH instruction stores a data in the stack.
PUSH <word data or word register>
POP instruction retrieves a data from the stack.
POP <word memory block or word register>
Examples:
PUSH AX
POP BX
Experiments
title Summation
.model small
.stack 100h
.data
msg db "Sum of numbers from 1 to 10 : $"
.code
main proc
mov ax,@data
mov ds,ax
lea dx,msg
mov ah,9
int 21h
mov ax,0
mov cx,10
AGAIN:
add ax,cx
loop AGAIN
call writeint
;Display AX as a decimal integer
mov ax,4C00h
int 21h
main endp
writeint proc
push ax
push bx
push cx
push dx
;Save used registers
test ax,8000h
jz L3
mov dl,'-'
push ax
mov ah,2
int 21h
pop ax
neg ax
;Test negative?
;Put - sign
;Get 2's complement
L3:
mov cx,0
mov bx,10 ;Write decimal
L1:
mov dx,0
div bx
add dx,'0'
push dx
inc cx
cmp ax,0
jne L1
mov ah,2
;Store digit
;Print char
L2:
pop dx
int 21h
loop L2
pop
pop
pop
pop
ret
writeint
dx
cx
bx
ax
;Restore digit
;Restore registers to previous values
endp
end main
Given code above calculates the sum of numbers from 1 to 10 and prints the result on the
screen.
Week 1 –
a) Modify the main procedure in the example such that the loop is constructed using
CMP and conditional jump instructions. Make a loop without using LOOP instruction.
b) Modify your code that you’ve written in (a) such that the program calculates the
sum of numbers from 5 to 10.
c) Modify your code that you’ve written in (a) such that the program calculates the
sum of numbers from 0 to 20 with the step 2. (0 + 2 + 4 + ... + 18 + 20)
Week 2 – Modify the main procedure to calculate factorial of the value stored in CX. Do not
try to compute the factorial of a number greater than 7. Because, the result won’t fit in 16-bits
register. Major steps of your code should be like this:
- Assign a number for example “5”, to CX
- Initialize AX register with the value “1” to compute the factorial.
- DX should be always zero. (DX:AX) = (0:AX) = AX
- Construct a loop to multiply the current value of AX with the value of
counter. You can use LOOP instruction for the loop, and MUL instruction for
multiplying. Note that you’ll need 16-bits multiplication.
- Display the result AX using “writeint” procedure. You need to use CALL
instruction. “writeint” procedure displays AX as an integer.
Report
Write a report about the procedure “writeint” given in the experiment. The content of your
report should be as follows:
1 – Your name, last name, student number.
2 – Write a brief explanation about how the procedure “writeint” works. What does it
do? What’s the algorithm used in it?
3 – What modifications are needed to make the procedure display the number in
binary or octal?
4 – Explain each instruction in the procedure one by one. You should answer
following questions about each instruction: What does it do? How does it work? Why
is it used? What’s the purpose of it?
You must submit your report within one month!
Good luck!
Download