In: Computer Science
Hi this is Assembly Language MASM x86 program. Please write it in the language and please explain it with comments thank you
Please answer it I really need help this question was refunded before so please answer. Thank you so much also these are two separate programs thank you.
1) Write a procedure to read in decimal or hex number (byte-sized) Then write a procedure using shifts and ANDS to convert the string to a binary number (if is backward, that ok)
2) Put the first (byte) 10 terms of the Fibonacci series into the byte array FIB 1 1 2 3 5 … Us num1 = 1 and num2 = 1 to start, NO other numbers allowed)
1) The code with detailed comments is given below:
assume cs:code, ds:data
;define data segment
data
segment
msg1 db "Enter the Hexadecimal number:$" ;
message to read a hexadecimal number
str db 3,0,3 dup(?) ;memory location storing
details on the number read
; byte sized hexadecimal number can hold 2 hex
characters. Hence max size of str is 3 where it can hold
; 2 characters with an additional space for
enter key.
msg2 db 10,13,"The binary representation is: $"
; message to display binary representation of the number
data ends
code segment
start:
mov ax, data
mov ds, ax ; initialization of data
segment
lea dx, msg1 ; display msg1
mov ah, 09h
int 21h
lea dx,str ; read the number using buffered
input
mov ah, 0Ah
int 21h
lea dx, msg2 ; display msg2
mov ah, 09h
int 21h
call HexToBin
mov ah, 4ch ; terminate the program
int 21h
HexToBin PROC
mov cl,str+1 ; in case of buffered input, the
length of input will be stored at location str+1
; cl register holds the length of the string
read
mov ch,00 ; ch stores the value of 0
lea si,str+2 ; the strating address of the
number read is stored in SI index register
; The following steps are repeated for every
character in the number read
up:
mov al,[si]; character is read from location
memory location pointed to by SI
; The ASCII values of Hex numbers can range from
30H to 39H and 41H to 46H
cmp al,39h; check if ascii value is below or
equal to 39h
; if the ascii code is <=39H then just and it
with 0FH to obtain numbers from 01 to 09H respectively
jbe next
; if ascii value id greater than 39H, then is
ranging from A to F
sub al,07h ; first subtact 7 from it to obtain
values in range 3AH to 3FH
; then AND it with 0FH to obtain 0AH to
0FH
next:
and al,0Fh
;we would like to have only the 4 digit binary
representation for each digit. So the higher order 4 bits is
; not necessary
shl al,4 ; first we shift contents of al left by
4 bit positions to remove the higher order 4
bits
mov bl,04; the remaining 4 bits is printed one
by one in a loop and bl is loop counter
print:
mov dl,30h ; dl stores the ascii code of 0
shl al,1 ; shift left is done to print the
binary representation if the proper order.
; If reverse representation is required, shift
right can be used.
adc dl,0; the shifted bit will be in the CF; if
there is a carry, adc will add 1 to dl else dl will remain
30H
; Hence, if there is carry, 1 will be printed on
screen and if there is no carry, 0 will be printed on screen
push ax ; push ax to prevent change in value of
al
mov ah,02h ; print the value in dl on
screen
int 21h
pop ax ; pop ax will restore value of al
dec bl ; decrement loop counter
jnz print; repeat loop until bl is zero
inc si ; to print next character, increment si
pointer
loop up ; outer loop continues until cx!=0
ret
HexToBin ENDP
code ends
end start
OUTPUT:
2) The program for computing 10 Fibonacci numbers is as follows:
data segment
num1 db 1
num2 db 1
fib db 10 dup(?)
data ends
code segment
start:
mov ax, data
mov ds, ax ; initialize the data
segment
lea si,fib ; make SI pointer point to the fib
byte array
mov al,num1 ; store num1 as 1st element in fib
array
mov [si],al
inc si ; increment si to point to next memory
location
;store num2 as 2nd element in fib array
mov al,num2
mov [si],al
inc si ; incement si pointer
mov cx,8 ; since two numbers are already stored,
8 more numbers will have to be generated.
up:
mov al,num1 ; mov num1 to al
add al,num2 ; add num2 to al
daa ; convert the result of hexadecimal addition
to decimal value by using DAA
mov [si],al ; store the result in location
pointed to bi SI
; perform, num1 = num2, num2 = al using register
bl
mov bl,num2
mov num1,bl
mov num2,al
inc si ; increment si pointer
loop up ; execute the loop until 8 fibonacci
numbers are generated.
mov ah, 4ch ; exit to operating system.
int 21h
code ends
end start
OUTPUT: