Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1 Write a program in Assembly that considers the following numbers: 541, 85

ID: 3783203 • Letter: P

Question

Part 1

Write a program in Assembly that considers the following numbers: 541, 85, -12, 26 and outputs, for each of them, their decimal, binary and hexadecimal form with following formatting:

541 00000000000000000000001000011101 0x0000021D

85 00000000000000000000000001010101 0x00000055

-12 11111111111111111111111111110100 0xFFFFFFF4

26 00000000000000000000000000011010 0x0000001A

That is, with a space between each representation for the same number, and with an empty line between each of the three numbers.

Part 2

Write a program in Assembly that, in a loop, reads a character C then outputs C-3, C and C+3 (basically the character together with its preceding and following ones by three). For each of them, you need to output their ASCII, decimal, hex and binary forms with formatting as in the following example. If C=‘I’ this is what your I/O window on MARS will display:

F 70 0x00000046 00000000000000000000000001000110

I 73 0x00000049 00000000000000000000000001001001

L 76 0x0000004C 00000000000000000000000001001100

Your program must repeat the acquisition & print in a loop as long as the following condition is met: the decimal value of the char user input must be within the range 36 to 123. If not, terminate the program. Basically, if you look at the ASCII table provided in class and on Blackboard, this means handling “usual” chars, representing numbers, letters, punctuation and common keyboard symbols.

Part 3

Write a program in Assembly that interacts with the user to perform age calculations. You will need to take from the user year, month and day and calculate with a certain precision (assuming Feb 1st 2017 now) the age then returns. Precision is: - For full credit, precision to the month; - For extra credit too, precision to the day as well (you must take into account leap years!!). Here’s a sample of I/O to further clarify the exercise (user input is highlighted in bold font):

y?

1992

m?

10

1992.10 – 2017.2: 24 yr 3 mon

d?

25

You lived 8865 days.

Explanation / Answer

The sample program to convert the given number into decimal and hexadecimal

In the beginning the data selection is very important .The data section should contain prompt and the “In binary, the number you entered will be : ” strings”.

org 100h
section .data
prompt1: db "Please enter a decimal number: $"
prompt2: db 0Dh, 0Ah, "In binary, the number you entered is: "
prompt3: db 0Dh, 0AHn "In hexadecimal, the number you entered is: "

Code the prompt for the user to input their number and to display the number.

section .text
start:
mov ah, 9 ; //prints the prompt
mov dx, prompt1 ; //tells what to print
int 21h      

We need to get the user input. We need to use a loop and read character by character until the character that the user has selected is the carriage return (Enter).

;input the value.
mov bx, 0 ; //bx holds input value
mov ah, 1 ; //input char function
int 21h ; //read character into al
top1: ; //while (char != Carriage return)
cmp al, 13 ;//is char = Carriage return?
je out1 ; // we're finished with input, jump to out
and ax, 000Fh ; //convert from ASCII to base 10 value
push ax ; //save it to stack
mov ax, 10 ; //set up to multiply bx by 10
mul bx ; // dx:ax=bx*10
pop bx ; //saved value in bx
add bx, ax ; //bx = old bx*10 + new digit
mov ah, 1 ; //input char function again for next digit
int 21h ; //read next char
jmp top1 ; //loop until cmp al, 13 = true
out1:

mov ah,9 ; //print binary output label
mov dx,prompt2 ; //this is specified in .data section
int 21h

we need to convert it to binary. This is done by using a “for” loop and bit rotation. We will initialize our count register, cx, to 16 in order to loop through our “top2” loop 16 times. We will be using int 21h function 2 in order to print single characters. Int 21h is a function to read the user input.

for 16 times do this:
mov cx, 16 ; //loop counter
top2:
rol bx, 1 ;//rotate most significant bit into carry flag
jc one ;//does carry flag = 1?
mov dl, '0' ;//if not, set up to print a 0
jmp print ;//now print it
one:
mov dl, '1' ;//printing a 1 if 0 is not true
print:
mov ah, 2 ;//print char fcn
int 21h ;//now print it
loop top2 ;//loop until done
mov cx, 4 ;//loop counter
top3:
rol bx, 4 ;//rotate top nybble into the bottom
mov dl, bl ; //put a copy in dl
and dl, 0Fh ;//we only want the lower 4 bits
cmp dl, 9 ; //is it in [0-9]?
ja AF ;//if not, process characters [A-F]
or dl, 30h ;//convert 0-9 to '0'-'9'
jmp print2 ;//now print
AF:
add dl, 55 ;//convert 10-15 to 'A'-'F'
print2:
mov ah, 2 ;//print character function
int 21h ;//print it
loop top3 ; //loop until done
Exit:
mov ah, 04Ch ;//DOS function: Exit program
mov al, 0 ;//Return exit code value
int 21h ; //Call DOS. Terminate Program

Compile it using command line: nasm -f bin filename.asm -o filename.com