You are on page 1of 36

Riphah International University Faisalabad

Computer Organization and Assembly Language


Assignment # 1

Submitted by: Submitted to:


Abdul Rameez Yasir Ijaz
Chapter 5 Manual
5.2.2 Section Review

1. A link library consists of assembly language source code.

Ans. False (it contains object code).

2. Use the PROTO directive to declare a procedure named


MyProc in an external link library.

Code example:
MyProc PROTO

3. Write a CALL statement that calls a procedure named


MyProc in an external link library.

Code example:
call MyProc

4. What is the name of the 32-bit link library supplied with this book?

Irvine32.lib.

5. Which library contains functions called from Irvine32.lib?

Kernel32.lib.

6. What type of file is kernel32.dll?

Kernel32.dll is a dynamic link library that is a fundamental part of the MS-Windows


operating system.

5.3.4 Section Review

1. Which procedure in the link library generates a random integer within a


selected range?
RandomRange procedure.
2. Which procedure in the link library displays "Press [Enter] to
continue. . ." and waits for the user to press the Enter key?
WaitMsg procedure.
3. Write statements that cause a program to pause for 700 milliseconds.
Code example:
mov eax,700
call Delay
4. Which procedure from the link library writes an unsigned integer to the
console window in decimal format?
WriteDec procedure
5. Which procedure from the link library places the cursor at a specific
console window location?
Gotoxy procedure.
6. Write the INCLUDE directive that is required when using the Irvine32
library.
INCLUDE Irvine32.inc.
7. What types of statements are inside the Irvine32.inc file?
PROTO statements (procedure prototypes) and constant definitions. (There
are also text macros, but they are not mentioned in this chapter.)
8. What are the required input parameters for the DumpMem procedure?
ESI contains the data's starting address, ECX contains the number of data
units, and EBX contains the data unit size (byte, word, or doubleword).
9. What are the required input parameters for the ReadString procedure?
EDX contains the offset of an array of bytes, and ECX contains the maximum
number of characters to read.
10. Which processor status flags are displayed by the DumpRegs
procedure?
Carry, Sign, Zero, Overflow, Auxiliary carry, and Parity.
11. Challenge: Write statements that prompt the user for an identification
number and input a string of digits into an array of bytes.
Code example:
.data
str1 BYTE "Enter identification number: ",0
idStr BYTE 15 DUP(?)
.code
mov edx,OFFSET str1
call WriteString
mov edx,OFFSET idStr
mov ecx,(SIZEOF idStr) - 1
call ReadString
5.4.3 Section Review
1. Which register (in protected mode) manages the stack?

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.

3. Why is the stack called a LIFO structure?

LIFO stands for "last in, first out." The last value pushed into the stack is the first value
popped out from the stack.

4. When a 32-bit value is pushed on the stack, what happens to ESP?

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.

False (you can push both 16-bit and 32-bit values)

7. Local variables in procedures are created on the stack.

True.

8. The PUSH instruction cannot have an immediate operand.

False (yes, it can, from the 80186 processor onward).

9. Which instruction pushes all of the 32-bit general-purpose registers on the


stack?

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:

PUSH EAX EBX ECX

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.

Equivalent to PUSH EAX:


sub esp,4
mov [esp],eax

5.5.6 Section Review

1. The PROC directive begins a procedure and the ENDP directive ends a
procedure.

True

2. It is possible to define a procedure inside an existing procedure.

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.

False (there is no NESTED operator).

9. In protected mode, each procedure call uses a minimum of 4 bytes of stack


space.

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.

True (it also receives a count of the number of array elements).

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.

The following statements would have to be modified:


add eax,[esi] becomes --> add ax,[esi]
add esi,4 becomes --> add esi,2

Chapter 6 Manual
6.2.10 Section Review

1. In the following instruction sequence, show the resulting value of AL where


indicated, in binary:
mov al,01101111b
and al,00101101b ; a.
mov al,6Dh
and al,4Ah ; b.
mov al,00001111b
or al,61h ; c.
mov al,94h
xor al,37h ; d.

(a) 0101101
(b) 01001000
(c) 01101111
(d) 10100011

2. In the following instruction sequence, show the resulting value of AL where


indicated, in hexadecimal:
mov al,7Ah
not al ; a.
mov al,3Dh
and al,74h ; b.
mov al,9Bh
or al,35h ; c.
mov al,72h
xor al,0DCh ; d.

(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.

(a)CF=0, ZF=0, SF=0


(b) CF=0, ZF=0, SF=0
(c) CF=1, ZF=0, SF=1

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

9. Write a single instruction that converts an ASCII digit in AL to its


corresponding binary value. If AL already contains a binary value (00h to 09h),
leave it unchanged.

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.

Find elements in SetX that are not found in SetY:


; Method 1: X - (X intersection Y)
mov eax,SetX ; X
mov edx,eax ; (X intersection Y)
and edx,SetY
sub eax,edx ; X - (X intersection Y)

; Method 2: (X union Y) - Y
mov eax,SetX
or eax,SetY ; X union Y
sub eax,SetY ; (X union Y) – Y

6.3.5 Section Review

1. Which jump instructions follow unsigned integer comparisons?


JA, JNBE, JAE, JNB, JB, JNAE, JBE, JNA

2. Which jump instructions follow signed integer comparisons?

JG, JNLE, JGE, JNL, JL, JNGE, JLE, JNG

3. Which conditional jump instruction branches based on the contents of ECX?

JECXZ

4. Are the JA and JNBE instructions equivalent? Explain your answer

Yes, because they both depend on the flag combinations of CF = 0 and ZF = 0

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

6. Which conditional jump instruction is equivalent to the JNA instruction?

JBE

7. Which conditional jump instruction is equivalent to the JNGE instruction?

JL

8. Will the following code jump to the label named Target?


mov ax,8109h
cmp ax,26h
jg Target

No (8109h is negative and 26h is positive).

9. Will the following code jump to the label named Target?


mov ax,-30
cmp ax,-50
jg Target

Yes

10. Will the following code jump to the label named Target?
mov ax,-42
cmp ax,26
ja Target

Yes (the unsigned representation of 42 is compared to 26)

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

6.4.3 Section Review


1. The LOOPE instruction jumps to a label when (and only when) the Zero flag is
clear

False.

2. The LOOPNZ instruction jumps to a label when ECX is greater than zero and
the Zero flag is clear.

True

3. The destination label of a LOOPZ instruction must be no farther than 128 or


127 bytes from the instruction immediately following LOOPZ.

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

5. Challenge: The LOOPNZ example in Section 6.4.2 relies on a sentinel value to


handle the possibility that a positive value might not be found. What might
happen if we removed the sentinel?

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.

6.5.5 Section Review

1. Implement the following pseudocode in assembly language:


if ebx > ecx then
X=1

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:

3. Implement the following pseudocode in assembly language:


if( val1 > ecx ) AND ( ecx > edx ) then
X=1
else
X=2

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.

4. Implement the following pseudocode in assembly language:


if( ebx > ecx ) OR ( ebx > val1 ) then
X=1
else
X=2

Code example:
cmp ebx,ecx
ja L1
cmp ebx,val1
ja L1
mov X,2
jmp next
L1: mov X,1
next:

5. Implement the following pseudocode in assembly language:


if( ebx > ecx AND ebx > edx) OR ( edx > eax ) then
X=1
else
X=2

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

7. Challenge: Rewrite the code from Section 6.5.3 so it is functionally equivalent,


but uses fewer instructions.

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.

2. In a finite-state machine diagram, what do the nodes represent?

Each node is a state.

3. In a finite-state machine diagram, what do the edges represent?

Each edge is a transition from one state to another, caused by some input.

4. In the signed integer finite-state machine (Section 6.6.2), which state is


reached when the input consists of "5"?

State C.

5. In the signed integer finite-state machine (Section 6.6.2), how many digits can
occur after a minus sign?

An infinite number of digits.

6. What happens in a finite-state machine when no more input is available and


the current state is a nonterminal state?

The FSM enters an error state.

7. Would the following simplification of a signed decimal integer finite-state


machine work just as well as the one shown in Section 6.6.2? If not, why not?

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.

You might also like