Uploaded by Tuấn Dũng

ECE391 Homework 4

advertisement
Ho Chi Minh City University of Technology
Department of Electronics and Electrical Engineering
ECE391: Computer System Engineering
Homework 4
1. For each part of this problem, answer the value of registers after the instruction is
executed.
No.
Before
Instruction
1
BX: FF 75
CX: 01 A2
mov bx, cx
2
AX: 01 A2
mov ax, 100
3
EDX: FF 75 4C 2E
Value: DWORD 1
4
AX: 01 4B
5
EBX: 00 00 3A 4C
Value: DWORD ?
6
EBX: FF FF FF 75
mov edx, Value
mov ah, 0
mov Value, ebx
EBX: FF FF FF 75
BX: 01 A2
CX: 01 A2
AX: 100
EDX: DWORD 1
Value: DWORD 1
AX:
EBX: 00 00 3A 4C
Value: 00 00 3A 4C
add ebx,ecx
EBX: 00 00 01 17
ECX: 00 00 01 A2
SF: 0, ZF:0, CF:0, OF:0
sub ebx,ecx
EBX: FF FF FD D3
ECX: 00 00 01 A2
7
After
ECX: 00 00 01 A2
ECX: 00 00 01 A2
SF: 0, ZF: 0, CF: 0, OF: 0
8
BX: FF 75
sub cx,bx
BX: FF 75
CX: FF FF 02 2D
SF: 1, ZF: 0, CF: 0, OF:1
CX: 01 A2
9
EAX: 00 00 00 64
sub eax,100
EAX:00, SF:0, ZF:1,
CF:0, OF:0
10
AX: 0A 20
add ax,Value
AX: 01 0C 40
SF:0, ZF:0, CF:0, OF:0
word at Value: FF 20
2. Write a complete 80x86 assembly language program to prompt for values of x, y, and z
and display the value of the expression x*2y + 4z. Allow for 16-bit integer values.
.model small
.stack 100h
.data
Prompt1 db “enter x: $”
Prompt2 db “enter y: $”
Prompt3 db “enter z: $”
x dw ?
y dw ?
z dw ?
.code
main proc
mov x,0
mov y,0
mov z,0
mov ax,@data
mov ds,ax
mov ah,9
lea dx,Promp1
int 21h
mov ah,1
int 21h
mov x,ax
mov ah,9
lea dx,Prompt2
int 21h
mov x,ax
mov ah,1
int 21h
mov y,ax
mov ah,9
lea dx,Prompt3
int 21h
mov ah,1
int 21h
mov z,ax
mov ax,x
mov bx,y
mul bx
mov bx,2
mul bx
mov bx,ax
mov ax,z
mov cx,4
mul cx
add ax,bx
3. Assume for each part of this problem that the EAX register contains 00 00 00 4F and the
doubleword referenced by value contains FF FF FF 38. Determine whether or not each of
the conditional jump statements causes a jump to dest.
(a) cmp eax,value
jl dest
NO JUMP
(b) cmp eax, value
jb dest
JUMP
(c) cmp eax, 04fh
je dest
NO JUMP
(d) cmp eax, 79
jne dest
JUMP
(e) cmp value, 0
jbe dest
NO JUMP
(f) cmp value, -200
jge dest
JUMP
(g) add eax, 200
js dest
NO JUMP
(h) add value, 200
jz dest
4. Write 80x86 assembly language code for the following C procedure:
int i; int
S=0;
for (i=0;i<10;i++)
{
S = S + i;
}
mov i,0
mov ax,s
loop: mov bx,i
cmp bx,10
je out_loop
add ax,bx
inc i
jmp loop
5. The greatest common divisor (GCD) of two positive integers m and n can be calculated
recursively by the function described below in pseudocode.
function GCD(m, n : integer) : integer;
if n = 0 then return m;
else
Remainder := m mod n;
return GCD(n, Remainder); end
if;
Implement this recursive definition in assembly language. Use the stack to pass the two
doubleword-size argument values. Return the value of the function in the EAX register.
The procedure should remove the parameters from the stack. Test your function with a
main program that inputs two integers, calls the greatest common divisor function GCD,
and displays the value returned.
mov ax,m
mov bx,n
jz comeback
Download