0% found this document useful (0 votes)
14 views5 pages

Semilore

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Semilore

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Olaniran Semilore Peter

Matric No: CSC/2019/156

Department: Computer Science with mathematics

Course Code: CPE 301

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

mov cx, 5 ; number of elements in vectors


mov al, 0 ; sum of vectorA
mov bl, 0 ; sum of vectorB

; Calculate sum of vectorA


mov si, 0 ; index for vectorA
sumA:
add al, [vectorA + si]
inc si
loop sumA

; Calculate sum of vectorB


mov si, 0 ; index for vectorB
sumB:
add bl, [vectorB + si]
inc si
loop sumB

; Calculate difference (result = sumB - sumA)


sub bl, al
mov [result], bl

; Print the result in binary


mov cx, 8
mov bl, [result]
printDiff:
mov ah, 2 ; print function
mov dl, '0'
test bl, 10000000b ; test first bit
jz zeroDiff
mov dl, '1'
zeroDiff:
int 21h
shl bl, 1
loop printDiff

; Print binary suffix


mov dl, 'b'
int 21h
; Wait for any key press
mov ah, 0
int 16h
ret

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.

mov cx, 10 ; Number of Fibonacci numbers to generate


mov ax, 0 ; First term (t0)
mov bx, 1 ; Second term (t1)

generate_fibonacci:
; Print the current term in the Fibonacci sequence
mov dl, ' '
int 21h
mov ax, bx
call print_ax

; Calculate the next term in the sequence: tn = tn-1 + tn-2


add bx, ax

; Decrement the counter


loop generate_fibonacci

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

; Convert the remainder to ASCII and print


add dl, '0'
mov ah, 2
int 21h
; Decrement the counter
loop print_digit
pop ax
ret

You might also like