Converting Binary to Octal Assembly Lang

纵饮孤独 提交于 2020-01-16 03:52:07

问题


First post, please be gentle. I was tasked with taking a binary number, converting it into an octal number, and displaying said octal number. I was given the code included below for reference, which converts to hex, but I cannot for the life of me find any documentation on how to convert from binary to octal. My peers are stumped as well, and any help or incite would be graciously appreciated. Thank You!

; program to do octal input and output
org 100h
section .data
prompt1: db "Please enter a decimal number: $"
prompt2: db 0Dh,0Ah, "The number in octal is:    $"
prompt3: db 0Dh,0Ah, "The number in decimal is:    $"

section .text
mov ah,9           ; print prompt
mov dx,prompt1
int     21h
call    dec_in      ; read value into bx
mov ah,9            ; print output label
mov dx,prompt2
int     21h 
call    hexout
mov ah,9            ; print output label
mov dx,prompt3
int     21h
call    dec_out     ; display the value in bx as hex
exit:
; exit to DOS
mov ax,4C00h        ; Normal Exit
int 21h             ; bye!

; dec_in will read a base 10 value from the keyboard and place it into the bx    register
dec_in: 
; save registers
push    ax
push    dx

xor bx,bx       ; bx holds accumulated input
mov ah,1        ; read char fcn
int 21h         ; read it into al
while1: 
cmp al,0Dh      ; char = CR?
je  finis       ; if so, we are done
push    ax      ; save the character read
mov ax,10       ; set up for multiply
mul bx          ; dx:ax <- bx * 10
mov bx,ax       ; put 16-bit result back in bx (assume no overflow)
pop ax          ; restore the char read
and ax,000Fh    ; convert character '0'-'9' to value 0-9
add bx,ax       ; add value to accumulated input
mov ah,1        ; read char fcn
int 21h         ; read next char into al
jmp while1      ; loop until done
finis:  
; restore registers
pop dx
pop ax
ret


; hexout will display the binary value in the bx register as a base 16 value    
hexout:
; save registers we will be using
push    ax
push    cx
push    dx
mov ah,2        ; display char fcn
mov cx,4        ; loop counter init
for1:               ; top of for loop
rol bx,4        ; rotate so digit is in lowest 4 bits
mov dl,bl       ; get low half in dl
and dl,0Fh      ;  and mask out all but 4 bits
cmp dl,9        ; dl <= 9?
jnbe    AtoF    ; if not, then it's A-F
or  dl,30h      ; convert 0-9 to '0'-'9'
jmp endif1      ; get ready to display
AtoF:   add dl,55   ; convert 10-15 to 'A'-'F'
endif1: int 21h     ; display char
loop    for1    ; loop until done
; restore registers
pop dx
pop cx
pop ax
ret

; dec_out will display the binary value in the bx register as a base 10 value   
dec_out:
; save registers we will be using
push    ax
push    bx
push    cx
push    dx

xor cx,cx       ; cx counts digits, initially zero
rept:
mov ax,bx       ; set up to divide by by 10
xor dx,dx       ; must have a 32 bit (unsigned) dividend
mov bx,10       ; divisor will be in bx
div bx          ; quotient will be in ax, remainder in dx
push    dx      ; push remainder on stack
inc cx          ; we generated another digit, so count it
mov bx,ax       ; the quotient goes back in bx
cmp ax,0        ; clever way to test if quotient is zero
jne rept        ; if not, generate next digit

mov ah,2        ; display character function
for2:               ; loop cx times
pop dx          ; pop digit to print
or  dl,30h      ; convert the digit to print to ASCII code
int 21h         ; display the character
loop    for2    ; and keep going until all digits displayed

; restore registers
pop dx
pop cx
pop bx
pop ax
ret

回答1:


First off, if you are looking for documentation about number base conversion, you should look at elementary school maths books ;) All you need is repeated division by the base to convert to any base.

Converting from binary to octal is however an easier special case, since 8 is a power of 2. Here, you can simply transform every 3 bits into an octal number.




回答2:


An octal number is just an representation of a binary number to make it shorter and more illustrative. Exact 3 bits are transformed to a octal digit.

Let's do it with the binary number 01101010

First of all group the number in triplets (like a decimal number): 01,101,010. The first group has only two digits, so add a leading zero: 001,101,010. Now convert it to decimal: 1,5,2. These decimal digits are octal digits as well, thus the octal result is 152.

If you've got a number in "computer form", you can access the binary digits directly by shifting or similar.



来源:https://stackoverflow.com/questions/33426221/converting-binary-to-octal-assembly-lang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!