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

Write an assembly program to translate an input number to its binary represen- t

ID: 3631918 • Letter: W

Question

Write an assembly program to translate an input number to its binary represen-
tation. Your program should repeatedly prompts the user for an input and display
the input in binary form, until the user chooses to quit.
Note: For positive number, do not show leading zeros..
Here is what the user will see when the program is executed:
This Program prints the binary equivalent of a decimal number
Please enter a number:78
Binary number = 1001110
Would you like to try again(Y/N):Y
Please enter a number:-78
Binary number = 11111111111111111111111110110010
Would you like to try again (Y/N)

Here are some more hints:

1. All numbers stored in register are in binary. So after read in 78 and 
load it to some register ie. %l0, the 32 bits of %l0 look exactly like 
00000000000000000000000001001110.

2. There is no print command that can print register content in binary 
representation. What you need to do is to test those bits one by one and 
output either 0 or 1 according to the test result.

3. An easy way to test a bit of a number n in its i-th (i=0,1,2,..., 
31)position is perform an "andcc" operation on n and another number with 
only one bit 1 in i-th position.
ie.
n=6 (0110 in binary), test its 1-th bit by:
0110 & 0010, the results is not equal to zero which means the 1-th bit 
of n is 1.


example

.section ".data"
input: .word 0 !where the user input number will be read in
yesNo: .byte 0 !store a character representing yes or no
nl: .asciz " " !where we dump the trailing newline input
format: .asciz "%d%c" !specify a decimal number (word) and a character
format2: .asciz "%c%c" !specify two characters
prompt: .asciz " Please enter a number:"
prompt2: .asciz " Would you like to try again(Y/N):"

!!!!!!!!!!!!!!!!!!!!!!!! CODES !!!!!!!!!!!!!!!!!!!!!!
.align 4
.global main
.section ".text"

main:
save %sp, -96, %sp

repeat:
! prompt the user for an input
set prompt, %o0
call printf
nop

set format, %o0 !what kind of data we want to get
set input, %o1 !location for the input number to be stored
set nl, %o2 !location to dump the input newline
call scanf
nop !now, the user input must be in the data block
!specified by the lable input and nl


set prompt2, %o0 !reprompt
call printf
nop

set format2, %o0 !getting a character and a newline
set yesNo, %o1 !location for yes/no
set nl, %o2 !dummy newline
call scanf
nop

set yesNo, %l0 !get the address of yes/no memory
ldub [%l0], %o0 !get the yes/no response from memory
cmp %o0, 'y'
be repeat !yes, then try again

cmp %o0, 'Y' !yes, then try again
be repeat
nop

ret !get out
restore

Explanation / Answer

.code
main proc
  mov ax,@data
  mov ds,ax
  
  mov ax,0
  mov bx,0
  mov cx,0
  mov dx,0
  mov print,0 ;
  mov count,0 ;
  mov num,0
  
  mov dx,offset msgstar ;
  mov ah,9
  int 21h
  
  mov dx,offset msgintro ;
  mov ah,9
  int 21h
  
  mov dx,offset msgstar ;
  mov ah,9
  int 21h
  
  
  mov dx,offset msginput ;
  mov ah,9
  int 21h
  
  mov cx,-1 ;

<strong class="highlight">input</strong>: mov ah, 00h
  int 16h
  cmp ah, 1ch
  je exit

<strong class="highlight">number</strong>: cmp al, '0'
  jb <strong class="highlight">input</strong>
  cmp al, '9'
  ja uppercase
  sub al, 30h
  call process
  jmp <strong class="highlight">input</strong>

uppercase: cmp al, 'A'
       jb <strong class="highlight">input</strong>
      cmp al, 'F'
   ja lowercase
   sub al, 37h
   call process
   jmp <strong class="highlight">input</strong>

lowercase: cmp al, 'a'
   jb <strong class="highlight">input</strong>
   cmp al, 'f'
   ja <strong class="highlight">input</strong>
   sub al, 57h
   call process
   jmp <strong class="highlight">input</strong>

   loop <strong class="highlight">input</strong>

process: mov ch, 4
   mov cl, 3
   mov bl, al

convert: mov al, bl
   ror al, cl
   and al, 01
   add al, 30h

   mov ah, 02h
   mov dl, al
   int 21h

   dec cl
   dec ch
   jnz convert

   mov dl, 20h
   int 21h
ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote