简体   繁体   English

将二进制转换为八进制汇编语言

[英]Converting Binary to Octal Assembly Lang

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

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. 但是,从二进制转换为八进制是一种更容易的特殊情况,因为8是2的幂。在这里,您可以将每3位简单地转换为八进制数。

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. 精确的3位转换为八进制数字。

Let's do it with the binary number 01101010 让我们以二进制数字01101010进行操作

First of all group the number in triplets ( like a decimal number ): 01,101,010. 首先将三胞胎中的数字分组( 如十进制数字 ):01101010。 The first group has only two digits, so add a leading zero: 001,101,010. 第一组只有两位数字,因此添加前导零:001,101,010。 Now convert it to decimal: 1,5,2. 现在将其转换为十进制:1,5,2。 These decimal digits are octal digits as well, thus the octal result is 152. 这些十进制数字也是八进制数字,因此八进制结果为152。

If you've got a number in "computer form", you can access the binary digits directly by shifting or similar. 如果您有“计算机形式”的数字,则可以通过移位或类似方法直接访问二进制数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM