Assignement # 1
Assignement # 1
Code example:
MyProc PROTO
Code example:
call MyProc
4. What is the name of the 32-bit link library supplied with this book?
Irvine32.lib.
Kernel32.lib.
ESP
2. How is the runtime stack different from the stack abstract data type?
The runtime stack is the only type of stack that is managed directly by the CPU. For
example, it holds the return addresses of called procedures.
LIFO stands for "last in, first out." The last value pushed into the stack is the first value
popped out from the stack.
ESP is decremented by 4.
5. Only 32-bit values should be pushed on the stack when using the Irvine32
library.
True.
6. Only 16-bit values should be pushed on the stack when using the Irvine16
library.
True.
PUSHAD.
10. Which instruction pushes the 32-bit EFLAGS register on the stack?
PUSHFD
11. Which instruction pops the stack into the EFLAGS register?
POPFD.
12. Challenge: Another assembler (called NASM) permits the PUSH instruction to
list multiple specific registers. Why might this approach be better than the
PUSHAD instruction in MASM? Here is a NASM example:
NASM's approach permits the programmer to be specific about which registers are to
be pushed. PUSHAD, on the other hand, does not have that flexibility. This becomes
important when a procedure needs to save several registers and at the same time
return a value to its caller in the EAX register. In this type of situation, EAX cannot be
pushed and popped because the return value would be lost.
13. Challenge: Suppose there were no PUSH instruction. Write a sequence of two
other instructions that would accomplish the same as PUSH EAX.
1. The PROC directive begins a procedure and the ENDP directive ends a
procedure.
True
False
3. What would happen if the RET instruction was omitted from a procedure?
Execution would continue beyond the end of the procedure, possibly into the beginning
of another procedure. This type of programming bug is often difficult to detect!
4. How are the words Receives and Returns used in the suggested procedure
documentation?
Receives indicates the input parameters given to the procedure when it is called.
Returns indicates what value, if any, the procedure produces when it returns it to its
caller.
5. The CALL instruction pushes the offset of the CALL instruction on the stack.
False (it pushes the offset of the instruction following the call).
6. The CALL instruction pushes the offset of the instruction following the CALL
on the stack.
True
7. The RET instruction pops the top of the stack into the instruction pointer.
True
8. Nested procedure calls are not permitted by the Microsoft assembler unless
the NESTED operator is used in the procedure definition.
True
10. The ESI and EDI registers cannot be used when passing parameters to
procedures.
False
11. The ArraySum procedure (Section 5.5.3) receives a pointer to any array of
doublewords.
12. The USES operator lets you name all registers that are modified within a
procedure.
True
13. The USES operator only generates PUSH instructions, so you must code POP
instructions yourself.
False
14. The register list in the USES directive must use commas to separate the
register names.
False
15. Which statement(s) in the ArraySum procedure (Section 5.5.3) would have to
be modified so it could accumulate an array of 16-bit words? Create such a
version of ArraySum and test it.
Chapter 6 Manual
6.2.10 Section Review
(a) 0101101
(b) 01001000
(c) 01101111
(d) 10100011
(a) 85h
(b) 34h
(c) BFh
(d) AEh
3. In the following instruction sequence, show the values of the Carry, Zero, and
Sign flags where indicated:
mov al,00001111b
test al,00000010b ; a.
mov al,00000110b
cmp al,00000101b ; b.
mov al,00000101b
cmp al,00000111b ; c.
4. Write a single instruction using 16-bit operands that clears the high 8 bits of
AX and does not change the low 8 bits.
and ax,00FFh
5. Write a single instruction using 16-bit operands that sets the high 8 bits of AX
and does not change the low 8 bits.
or ax,0FF00h
6. Write a single instruction (other than NOT) that reverses all the bits in EAX.
xor eax,0FFFFFFFFh
7. Write instructions that set the Zero flag if the 32-bit value in EAX is even and
clear the Zero flag if EAX is odd.
or al,00100000b
8. Write a single instruction that converts an uppercase character in AL to
lowercase but does not modify AL if it already contains a lowercase letter.
or al,00100000b
and al,00001111b
10. Write instructions that calculate the parity of the 32-bit memory operand. Hint:
Use the formula presented earlier in this section: B0 XOR B1 XOR B2 XOR B3.
Code example:
.data
memVal DWORD ?
.code
mov al,BYTE PTR memVal
xor al,BYTE PTR memVal+1
xor al,BYTE PTR memVal+2
xor al,BYTE PTR memVal+3
11. Given two bit-mapped sets named SetX and SetY, write a sequence of
instructions that generate a bit string in EAX that represents members in SetX
that are not members of SetY.
; Method 2: (X union Y) - Y
mov eax,SetX
or eax,SetY ; X union Y
sub eax,SetY ; (X union Y) – Y
JECXZ
5. Suppose the CMP instruction compares the integers 7FFFh and 8000h. Show
how the JB and JL instructions would generate different results if used after
comparing these values.
If JL is used after comparing 7FFFh to 8000h, the operands will be processed as signed
values (+32,767 and 32,768), so the jump will not be taken. If JB is used instead, the
values will be considered unsigned, and the jump will be taken
JBE
JL
Yes
10. Will the following code jump to the label named Target?
mov ax,-42
cmp ax,26
ja Target
11. Write instructions that jump to label L1 when the unsigned integer in DX is
less than or equal to the integer in CX.
Code:
cmp dx,cx
jbe L1
12. Write instructions that jump to label L2 when the signed integer in AX is
greater than the integer in CX.
Code:
cmp ax,cx
jg L2
13. Write instructions that first clear bits 0 and 1 in AL. Then, if the destination
operand is equal to zero, the code should jump to label L3. Otherwise, it
should jump to label L4.
Code:
and al,11111100b
jz L3
jmp L4
False.
2. The LOOPNZ instruction jumps to a label when ECX is greater than zero and
the Zero flag is clear.
True
True
4. Modify the LOOPNZ example in Section 6.4.2 so that it scans for the first
negative value in the array. Change the array initializers so they begin with
positive values.
Code example:
.data
array SWORD 3,5,14,-3,-6,-1,-10,10,30,40,4
sentinel SWORD 0
.code
main PROC
mov esi,OFFSET array
mov ecx,LENGTHOF array
next:
test WORD PTR [esi],8000h ; test sign bit
pushfd ; push flags on stack
add esi,TYPE array
popfd ; pop flags from stack
loopz next ; continue loop while ZF=1
jz quit ; none found
sub esi,TYPE array ; ESI points to value
If a matching value were not found, ESI would end up pointing beyond the end of the
array. By pointing at an undefined memory location, a program runs the risk of causing
a runtime error.
Code example:
cmp ebx,ecx
jna next
mov X,1
next:
2. Implement the following pseudocode in assembly language:
if edx <= ecx then
X=1
else
X=2
Code example:
cmp edx,ecx
jnbe L1
mov X,1
jmp next
L1: mov X,2
next:
Code example:
cmp val1,ecx
jna L1
cmp ecx,edx
jna L1
mov X,1
jmp next
L1: mov X,2
next:
4.
5.
6.
7.
Code example:
cmp ebx,ecx
ja L1
cmp ebx,val1
ja L1
mov X,2
jmp next
L1: mov X,1
next:
Code example:
cmp ebx,ecx ; ebx > ecx?
jna L1 ; no: try condition after OR
cmp ebx,edx ; yes: is ebx > edx?
jna L1 ; no: try condition after OR
jmp L2 ; yes: set X to 1
;-----------------OR(edx > eax) ------------------------
L1: cmp edx,eax ; edx > eax?
jna L3 ; no: set X to 2
L2: mov X,1 ; yes:set X to 1
jmp next ; and quit
L3: mov X,2 ; set X to 2
next:
6. In the program from Section 6.5.4, why is it better to let the assembler
calculate NumberOfEntries rather than assigning a constant such as
NumberOfEnteries 4?
Future changes to the table will alter the value of NumberOfEntries. We might forget to
update the constant manually, but the assembler can correctly adjust a calculated value
Code example:
.data
sum DWORD 0
sample DWORD 50
array DWORD 10,60,20,33,72,89,45,65,72,18
ArraySize = ($ - Array) / TYPE array
.code
mov eax,0 ; sum
mov edx, sample
6.6.3Section Review
1. A finite-state machine is a specific application of what type of data structure?
A directed graph.
Each edge is a transition from one state to another, caused by some input.
State C.
5. In the signed integer finite-state machine (Section 6.6.2), how many digits can
occur after a minus sign?
No. The proposed FSM would permit a signed integer to consist of only a plus () or
minus () sign. The FSM in Section 6.6.2 would not permit that.