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

Assembly 将二进制汇编语言转换为八进制汇编语言,assembly,x86,dos,octal,Assembly,X86,Dos,Octal,第一个帖子,请温柔一点。我的任务是获取二进制数,将其转换为八进制数,并显示所述八进制数。我得到了下面包含的代码作为参考,它可以转换为十六进制,但我找不到任何关于如何从二进制转换为八进制的文档。我的同龄人也被难倒了,任何帮助或煽动都将不胜感激。谢谢大家! ; program to do octal input and output org 100h section .data prompt1: db "Please enter a decimal number: $" prompt2: db 0D

第一个帖子,请温柔一点。我的任务是获取二进制数,将其转换为八进制数,并显示所述八进制数。我得到了下面包含的代码作为参考,它可以转换为十六进制,但我找不到任何关于如何从二进制转换为八进制的文档。我的同龄人也被难倒了,任何帮助或煽动都将不胜感激。谢谢大家!

; 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
;进行八进制输入和输出的程序
组织100小时
第二节数据
提示1:db“请输入十进制数:$”
提示2:db 0Dh,0Ah,“八进制的数字是:$”
提示3:db 0Dh,0Ah,“十进制数字为:$”
第节.案文
mov-ah,9岁;打印提示
mov dx,提示1
int 21h
打电话给dec_;将值读入bx
mov-ah,9岁;打印输出标签
mov dx,prompt2
int 21h
呼叫hexout
mov-ah,9岁;打印输出标签
mov dx,prompt3
int 21h
把dec_叫出去;将bx中的值显示为十六进制
出口:
; 退出待办事项
mov-ax,4C00h;正常出口
int 21h;再见!
; dec_in将从键盘读取一个以10为基数的值,并将其放入bx寄存器
十二月份:
; 保存寄存器
推斧
推送dx
异或bx,bx;bx保存累积输入
mov-ah,1;读字符fcn
int 21h;把它读给艾尔听
第1部分:
cmp-al,0Dh;char=CR?
杰菲尼斯;如果是这样,我们就完了
推斧;将字符保存为已读
mov-ax,10;设置为乘法

mul-bx;dx:ax首先,如果您要查找有关数基转换的文档,您应该查看小学数学书籍;)您只需重复除以基数即可转换为任意基数


然而,从二进制到八进制的转换是一个更容易的特例,因为8是2的幂。在这里,您可以简单地将每3位转换为一个八进制数。

八进制数只是二进制数的表示形式。精确的3位转换为八进制数字

让我们用二进制数011010来做

首先,将数字分成三组():011010。第一组只有两个数字,因此添加前导零:001101010。现在将其转换为十进制:1,5,2。这些十进制数字也是八进制数字,因此八进制结果是152

如果你有一个“计算机形式”的数字,你可以通过移位或类似的方式直接访问二进制数字