COAL Final - Fall20 (Solution)
COAL Final - Fall20 (Solution)
School of Computing,
Final Examinations, Fall 2020
8th January, 2021, 03:00 pm – 06:00 pm.
Course Code: EE 229 Course Name: Computer Organization and Assembly Language
Instructors: Dr. Muhammad Nouman Durrani, Muhammad Danish Khan, Shoaib Rauf
Student’s Roll No: Section:
SOLUTION
PART – I
Short Questions
(ii) How many bytes are allocated as a result of the following data definition directive:
2400 BYTES
The first instruction copies the base address of X1 in EBX, second instruction
copies the contents.
(iv) Elaborate the difference between MOVZX and MOVSX instructions through an example.
(v) Consider the following code. What is the value of AX after execution of the following instructions?
MOV AX, 10H
MOV CX, 0AH
L1:
INC AX
DEC CX
Page 1 of 8
LOOPNE L1
AX = 15h
(vi) Consider the following code snippet. What is the value of AX after execution of the following instructions?
AX = A90Fh
(vii) Elaborate the difference between RCR and ROR with the help of working example.
ROR (Rotate Right) Instruction shifts each bit to the right and copies the
lowest bit into the Carry flag and the highest bit position (MSB).
The RCR (rotate carry right) instruction shifts each bit to the right, copies
the Carry flag into the MSB, and copies the LSB into the Carry flag:
(viii) Write equivalent x86 assembly instructions for the following operation:
POP EBX
(ix) When does RECURSION is preferred over LOOPING? Give some real world example.
Free Responses.
(x) Write the x86 assembly PROTOYPE for following sample function:
void sample(int length, char ch, int arr[]);
(xi) Write a valid x86 assembly INVOKE instruction for the following sample function:
int sample(int arr[], int num, int* x)
(xii) Write a valid x86 assembly function signature using PROC for the following sample function:
void sample(char ch, int num, char chArr[])
(xiii) What will happen, if immediately upon entering a subroutine you execute a “POP” instruction?
The subroutine will lose its return address.
(xiv) Write a single x86 instruction to replace each of the following two instructions:
Page 2 of 8
LEA EBX, X1
MOV EBX, OFFSET X1
LOADSB
MOV AL, [ESI]
(xv) When do you typically use the CBW and CWD instructions?
With 8bits, and 16bits signed divisions respectively.
(xvi) Assume a four-byte x86 machine code as B9 C5 12 00. How would you determine the size of register used in this
instruction? Explain.
The W-bit(0th bit) of Opcode byte determines the size of register in this
instruction.
(xvii) What are the key benefits of RISC architecture (like MIPS)?
Faster
Lesser Complexity
(xviii) In MIPS, how 16 bit value is loaded from a memory location into the destination register? How a specified immediate
value is loaded into a destination register? Give Instruction examples only.
LH $12, 80($3)
LI $12, 100
(xix) Draw and briefly discuss the general memory layout of a program in MIPS architecture.
(xx) Explain the execution steps of MIPS instruction BNEQ $t0, $t1, L1. Assuming L1 as a valid label.
Page 3 of 8
PART – II
Descriptive Questions
L1: PUSH CX
CMP [ESI], 1
JE isPrime
MOV CX, [ESI]
MOV BX, CX
DEC CX
(ii) Write a short code segment, using the LOOP instruction that calculates AVERAGE of an integer array intArray in EAX.
Page 4 of 8
void main(){
int sum = 0;
int index = 0;
func1 (index, &sum);
}
.data
Array DWORD 10, 60, 20, 33, 72,89,45,65,72,18
Sample DWORD 50
main PROC
PUSH EBP
MOV EBP, ESP
SUB ESP, 8 ; allocating space for LOCAL DATA
MOV [EBP-4], 0 ; sum
MOV [EBP-8], 0 ; index
INVOKE func1, [EBP-8], EBP-4
MOV ESP, EBP ; cleaning main’s LOCAL DATA
POP EBP
RET
ENDP main
Func1 PROC, i: DWORD, s: PTR DWORD
PUSH EBP
MOV EBP, ESP
SUB ESP, 4 ; arraySize
MOV ESI, s
MOV EAX, sizeof ARRAY
MOV EDX, 0
MOV ECX, sizeof Sample
DIV ECX
MOV [EBP-4], EAX ; arraySize =sizeOfArray/sizeOf sample
MOV EBX, i
L1: CMP [EBP-4], EBX
JA continue
MOV EBX, sample
Page 5 of 8
CMP array[i], EBX
JA continue
MOV ECX, array[i]
ADD [ESI], ECX
Continue: INC i
……
.
MOV ESP, EBP
POP EBP
RET 8
Func1 ENDP
(ii) Given that EBX points to the following array ewords, write x86 code snippet to process this array and print the starting offset
of each word starting with letter e or E.
ewords byte “The eagle eyed snake eagerly attacked Easter eggs”, 0
MOV ESI, OFFSET ewords
MOV ECX, LENGTHOF ewords
DEC ECX,
L1: CMP [ESI], ‘E’
JE condition2
CMP [ESI], ‘e’
JE condition2
JMP continue
condition2: CMP [ESI-1], ‘ ‘
JNE continue
MOV EDX, ESI
; call display
Continue: INC ESI
Loop L1
Page 6 of 8
Question No. 5 Encode the following x86 assembly instructions [2 x 4 = 8 Points]
(i) CMP BX, [BP+08h]
3B 01 011 110 <- 08
= 3B 5E 08h
(ii) MOV [BX+4099h], 4F87h
C7 10 000 111 <-99 40 <- 874F
= C7 87 9940 874Fh
(iii) SUB BX, 7E90h
81 + 03 <- 907E
= 84 907Eh
(iv) INC WORD PTR [SI+2A6h]
FF 10 000 100 <- A6 02
= FF 84 A6 02h
Solution:
I1 IF ID OF WB
I2 IF ID OF MEM WB
I3 IF ID OF WB
I4 IF ID OF MEM WB
I5 IF ID OF WB
(ii) Now resolve the data hazards by RESCHEDULING and structural hazards by STALLING. Draw the updated pipeline of the
rescheduled instructions. How many cycles are required, if the above code is rescheduled.
Solution:
Page 7 of 8
LW $11, 100($2)
OR $10, $2, $9
NOR $8, $6, $13
***STAY BRIGHT***
Page 8 of 8