Assembly Language Programming: Code Analysis and Solutions
Assembly Language Code Analysis and Solutions
Section 1: Instruction Correctness Check
.data
calculate WORD 100
wVal DWORD 2Instructions:
mov bl, calculatemov ax, wVal
Question 1: Are the above two instructions correct? If not, justify your claim. [2 marks]
Answer:
Both instructions are incorrect.
- For the
movoperation, the operands must generally be of the same size. - In instruction 1,
blis 8 bits (1 byte), butcalculateis defined as aWORD(16 bits or 2 bytes). The operand sizes do not match. - In instruction 2,
axis 16 bits (2 bytes), butwValis defined as aDWORD(32 bits or 4 bytes). The operand sizes do not match.
Section 2: Loop Pseudo-code
Question 2: Write the pseudo-code for the loop that calculates the sum of the integers 3 + 2 + 1. [2 marks]
Answer:
mov ax, 0 ; Initialize accumulator (sum)
mov ecx, 3 ; Initialize counter
L1:
add ax, cx ; Add current counter value to sum
loop L1 ; Decrement ECX and jump to L1 if ECX is not zeroSection 3: Flag Register Status
Question 3: What will be the values of the Zero Flag (ZF) after executing the following code? [1 mark]
bValue BYTE 42
MOV AL, bValue
SUB AL, 2AhAnswer:
The operation is $42_{10} - 2Ah_{16}$. Since $42_{10} = 2Ah_{16}$, the result of the subtraction is 0.
ZF = 1
Section 4: Subtraction Results and Flags
Question 4: Fill in the blanks. [2 marks]
mov al, 1
sub al, 2 ; AL = -1, CF = 1mov eax, 3
sub eax, 4 ; EAX = -1, SF = 1Section 5: Array Element Access
Question 5: Write the Assembly Language instructions to access the 5th element of the following arrays using indexed operand and the TYPE operator. [3 Marks]
.data
arrayB BYTE 0, 1, 2, 3, 4, 5
arrayW WORD 0, 1, 2, 3, 4, 5
arrayD DWORD 0, 1, 2, 3, 4, 5Answer:
.code
mov esi, 4 ; Index for the 5th element (0-based)
mov al, arrayB[esi * TYPE arrayB] ; Accesses 5th BYTE element
mov bx, arrayW[esi * TYPE arrayW] ; Accesses 5th WORD element
mov edx, arrayD[esi * TYPE arrayD] ; Accesses 5th DWORD elementNote: Quiz 2 consists of short questions due to time constraints. Expect questions similar to the following samples in the final examination from Chapter 4.
Sample Questions for Final Examination from Chapter 4
Extra Sample Question 1: Implement the following C-like code using Assembly.
Sval = -Tval + (Pval + Qval)
Assume any data type for Sval, Tval, Pval, and Qval, and initialize values.
Answer:
.data
Sval SDWORD ?
Tval SDWORD 26
Pval SDWORD 30
Qval SDWORD 40
.code
mov eax, Tval
neg eax ; EAX = -Tval
mov ebx, Pval
add ebx, Qval ; EBX = Pval + Qval
add eax, ebx ; EAX = -Tval + (Pval + Qval)
mov Sval, eaxExtra Sample Question 2: Write the necessary Assembly code segment to add the elements of the following array using Indirect Addressing.
arrayD DWORD 1000h, 2000h, 3000h
Preferable answer using Loop and Indexed Scaling:
Answer:
.data
arrayD DWORD 1000h, 2000h, 3000h
.code
mov esi, 0
mov eax, 0
mov ecx, LENGTHOF arrayD
Add:
add eax, arrayD[esi * TYPE arrayD] ; Add element using scaled index
inc esi
loop Add ; EAX holds the sumAlternative answer (Manual Unrolling):
.data
arrayD DWORD 1000h, 2000h, 3000h
.code
mov esi, OFFSET arrayD
mov eax, [esi] ; EAX = arrayD[0]
add esi, 4 ; Move to next DWORD address
add eax, [esi] ; EAX = EAX + arrayD[1]
add esi, 4 ; Move to next DWORD address
add eax, [esi] ; EAX = EAX + arrayD[2] ; EAX = sum of the arrayExtra Sample Question 3: Write the Assembly Language Code that can copy a source string using a loop.
Answer:
.data
source BYTE "This is the source string", 0
target BYTE SIZEOF source DUP(0)
.code
mov esi, 0 ; Index register (Source Index)
mov ecx, SIZEOF source ; Loop counter
L1:
mov al, source[esi] ; Get character from source
mov target[esi], al ; Store it in the target
inc esi ; Move to next character
loop L1 ; Repeat for entire string
English with a size of 4.63 KB