Semilore
Semilore
PART TWO
Activity 1
The provided assembly code is a program written for the x86 architecture, specifically for DOS.
It calculates the sum of elements in a vector, stores the result in variable m, and then prints the
result in binary and decimal format.
Here's a breakdown of the code:
Initialization:
Sets up the organization of the code as a COM file (org 100h).
Initializes cx with the number of elements in the vector (mov cx, 5).
Initializes al (accumulator for sum) to zero (mov al, 0).
Initializes bx (index) to zero (mov bx, 0).
Summation Loop:
Loops through the elements of the vector, adding each element to al.
Uses the loop instruction to repeat the process until cx becomes zero.
Storing Result and Printing in Binary:
Stores the result in variable m (mov m, al).
Prints the binary representation of the result using the DOS interrupt int 21h. It shifts through the
bits of bl and prints '0' or '1' accordingly.
Printing Decimal Result:
Calls the print_al procedure to print the decimal representation of the result.
Waiting for Key Press:
Waits for any key press using the DOS interrupt int 16h.
Subroutine print_al:
Prints the decimal representation of the value in al.
It uses BIOS interrupt int 10h for character output.
Variables:
vector is an array of bytes with values 5, 4, 5, 2, 1.
m is a byte to store the sum.
The print_al procedure is defined to print the decimal representation of al.
End of Program:
ret instruction indicates the end of the program.
Activity 4
Modify the program to calculate the difference (in binary) between the sums of vectors A and B
A: 1, 3, 5, 7, 9 B: 0,2,4,6,8,10 i.e ∑B - ∑A
name "calc-diff"
org 100h ; directive make tiny com file.
; A: 1, 3, 5, 7, 9
vectorA db 1, 3, 5, 7, 9
; B: 0, 2, 4, 6, 8, 10
vectorB db 0, 2, 4, 6, 8, 10
; Result variable
result db 0
.code
Activity 5
Modify the program to generate the
Fibonacci sequence {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …}using the algorithm below
Show your output to the Lab Demonstrator for grading tn = tn-1 + tn-2 where t0 = 0 and t1 = 1
Solution:
name "fibonacci"
org 100h ; directive to make a tiny COM file.
generate_fibonacci:
; Print the current term in the Fibonacci sequence
mov dl, ' '
int 21h
mov ax, bx
call print_ax
exit_program:
; Wait for any key press before exiting
mov ah, 0
int 16h
ret
print_ax:
; Print the value in AX
push ax
mov cx, 10 ; Set the counter to 10 (number of digits)
mov ah, 0 ; Initialize AH to 0
print_digit:
; Divide AX by 10, obtaining quotient in AX and remainder in DX
mov dx, 0
div cx