Assembly Language Programming: Code Analysis and Solutions

Posted by Anonymous and classified in Computers

Written on in English with a size of 4.63 KB

Assembly Language Code Analysis and Solutions

Section 1: Instruction Correctness Check

.data

calculate WORD 100
wVal DWORD 2

Instructions:

  1. mov bl, calculate
  2. mov ax, wVal

Question 1: Are the above two instructions correct? If not, justify your claim. [2 marks]

Answer:

Both instructions are incorrect.

  • For the mov operation, the operands must generally be of the same size.
  • In instruction 1, bl is 8 bits (1 byte), but calculate is defined as a WORD (16 bits or 2 bytes). The operand sizes do not match.
  • In instruction 2, ax is 16 bits (2 bytes), but wVal is defined as a DWORD (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 zero

Section 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, 2Ah

Answer:

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 = 1
mov eax, 3
sub eax, 4 ; EAX = -1, SF = 1

Section 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, 5

Answer:

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

Note: 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, eax

Extra 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 sum

Alternative 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 array

Extra 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

Related entries: