Key to the midterm questions

advertisement
KEY TO ICS MIDTERM
Fall 2009
NOTE. There can be several other correct answers to some of these problems.
1. A main program calls sub1 in this manner:
push X
push Y
push offset z
call sub1
where X, Y, Z are all word variables.
Sub1 should set Z to X – Y. Supply the code for Sub1 using the standard
method for subroutines.
ANSWER
title sub1
public sub1
.model small
.code
sub1 proc near
push bp
mov bp, sp
mov ax, [bp+8]
sub ax, [bp+6]
mov bx, [bp+4]
mov [bx], ax
pop bp
ret 6
sub1 endp
end
Comment: The third argument pushed onto the stack was the offset of z.
It is not correct to employ
mov [bp+4], ax
to store the answer in z. This just stores the answer on the stack
at the place containing the offset
2. Write Assembler code equivalent to the following high-order language
statement:
if (A=B or C=D)
M=N
else
M=K
Assume that A, B, C, D, M, N, K are all word variables
ANSWER.
mov ax, A
cmp ax, B
je truecase
mov ax, C
cmp ax, D
je truecase
;false case follows
mov ax, K
mov M, ax
jmp continue
truecase:
mov ax, N
mov M, ax
continue:
Comment: Incidentally, employing “C” as a variable name causes problems,
as it’s a keyword used in interfacing with programs written in C.
It would be better to employ e.g. “C1” instead of “C”.
3. Write a macro called POWER with a single argument N, which takes he
number in Ax and raises it to the power of N, where N is a positive number.
The result should be stored in AX. Assume that the result fits in 16 bits.
ANSWER.
power macro n
local again
mov bx, ax
mov cx, n
dec cx
again:
imul bx
loop again
endm
4. Given
AREA DB 64000 DUP(?)
write code using string manipulation instructions to copy the contents of AREA
to video graphics memory. Recall that this memory starts at absolute
address A0000h. Your code should not contain any loops.
ANSWER.
mov ax, 0a000h
mov es, ax
mov di, 0
lea
si, AREA
mov cx, 64000
cld
rep movsb
5. Write code to enter Graphics Mode 13h, and then draw a purple square,
with each of its sides 100 pixels long, and its left hand top corner at col 30,
row 50. the color code for purple is 5h. Any macros that you employ must be
provided as part of your answer.
ANSWER.
mov ah, 0
mov al, 13h
int 10h
mov ax, 0a000h
mov es, ax
cld
;upper horizontal line
mov cx, 100
mov di, 320*50 + 30
mov al, 5
rep stosb
;lower horizontal line
mov
cx, 100
mov
di, 320*150 + 30
rep stosb
;left vertical line
mov
cx, 100
mov
di, 320*50 + 30
LeftVert:
mov
byte ptr es:[di], 5
add
di, 320
loop LeftVert
;right vertical line
mov
cx, 100
mov
di, 320*50 + 130
RiteVert:
mov
byte ptr es:[di], 5
add
di, 320
loop RiteVert
;wait for a key press & then restore mode 3 (not requested in question 5)
mov ah, 1
int 21h
mov ah, 0
mov al, 3
int 10h
Download