0% found this document useful (0 votes)
4 views

cs401 assignment 2 sol

Uploaded by

larec48311
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)
4 views

cs401 assignment 2 sol

Uploaded by

larec48311
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/ 3

CS401 Assignment 2

Solution Fall 2024

Student Id:BC220412988

Task: [20 marks]

Write a program that will sum your VU Student ID (excluding the first two alphabets)
and then determine if the result is even or not.

Details:

I Store your Student ID, excluding the first two alphabets, in an array. For example,
if your Student ID is BC123456789 then create an array that will have 1, 2, 3, 4, 5, 6, 7,
8, 9 as its elements.

II When calculating the sum, there is no need to use counter register. You can
directly perform calculations by accessing elements from array and adding them in the
accumulator register.

III When determining if the resultant sum is even, use appropriate bitwise operator
and jump instructions.

IV If result is even, store 1 in DX register; otherwise store 0.

Screenshot Requirements:

Provide AFD screenshot after you have successfully run your code.

In the screenshot:

I Memory area 1 must show the sum of your Student ID digits.

II The value of DX register should indicate if sum is even or odd (either 1 or 0)


Code:
org 0x0100 ; Program starts at offset 0x0100

jmp start ; Jump to start of program

; Task: Store Student ID digits, calculate sum, and check if even or odd

st db 2,2,0,4,1,2,9,8,8 ; Student ID digits excluding "BC" (1-9)

sum db 0 ; Memory location for storing sum of digits

res db 0 ; Result to store 0 (odd) or 1 (even)

start:

; Initialize sum to 0

xor ax, ax ; Clear AX (used for summing)

lea si, [st] ; Load address of the studentID array into SI

mov cx, 9 ; We have 9 digits in the student ID (1-9)

sum_digits:

; Add current digit to sum (AL register is used here, which is 8-bit)

add al, [si] ; Add the current digit from studentID array to AL

inc si ; Move to the next digit in the array

loop sum_digits ; Repeat until all digits are summed

; Store the result in 'sum' (the sum of digits in AL)

mov [sum], al ; Store the sum in the sum variable (Memory Area 1)

; Check if the sum is even or odd using bitwise AND

test al, 1 ; Test if the least significant bit is set (odd/even check)

jz even ; If zero, the sum is even, jump to even label


; If odd, store 0 in DX (odd)

mov dx, 0

jmp done

even:

; If even, store 1 in DX (even)

mov dx, 1

done:

; Exit program (return control to DOS)

mov ah, 4c00h ; DOS interrupt to terminate program

int 21h ; Call Dos interrupt to exit the program

Screenshot:

You might also like