转换二进制到八朗大会 [英] Converting Binary to Octal Assembly Lang

查看:176
本文介绍了转换二进制到八朗大会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次发帖,请温柔。我用取二进制数,将其转换成一个八进制数,以及显示所述的八进制数的任务。我得到了以下供参考,这转换为十六进制的code,但我不能为我的生活找到如何从二进制转换八进制任何文件。我的同龄人难住了,以及,任何帮助或煽动会欣然AP preciated。谢谢!

 ;程序做八进制的输入和输出
组织100H
段.data
PROMPT1:DB请输入一个十进制数:$
PROMPT2:DB 0DH,0AH,八进制的数字是:$
prompt3:DB 0DH,0AH,十进制数为:$.text段
MOV啊,9;打印提示
MOV DX,PROMPT1
INT 21H
调用dec_in;读值进入BX
MOV啊,9;输出打印标签
MOV DX,PROMPT2
INT 21H
调用HEXOUT
MOV啊,9;输出打印标签
MOV DX,prompt3
INT 21H
调用dec_out;在BX为十六进制显示值
出口:
;退出到DOS
MOV AX,4C00h;正常退出
INT 21H;再见!; dec_in会从键盘读取一个基地10值,将其放入BX寄存器
dec_in:
;保存寄存器
推斧
推DX异或BX,BX; BX拥有累计输入
MOV啊,1;读取字符FCN
INT 21H;读入人
while1:
CMP人,0DH;字符= CR?
JE菲尼什;如果是这样,我们已经完成
推斧;保存读取字符
MOV AX,10;设立乘法
MUL BX; DX:AX< - BX * 10
MOV BX,AX;把16位的结果早在BX(假定没有溢出)
流行斧;恢复读取字符
和斧头,000FH;转换字符'0' - '9'珍惜0-9
加BX,AX;增加价值累计输入
MOV啊,1;读取字符FCN
INT 21H;读下一个字符到人
JMP while1;循环,直到DONE
菲尼什:
;恢复寄存器
流行的DX
流行斧
RET
; HEXOUT将显示在BX寄存器中的二进制值作为基础值16
HEXOUT:
;保存我们将要使用的寄存器
推斧
推CX
推DX
MOV啊,2;显示字符FCN
MOV CX,4;循环计数器初始化
FOR1:;对循环顶部
ROL BX,4;旋转,使数字是低4位
MOV DL,BL;得到DL低一半
和DL,的0Fh;并屏蔽掉所有,但4位
CMP DL,9; DL< = 9?
jnbe ATOF;如果没有,那么它的A-F
或DL,30H; 0-9转换为'0' - '9'
JMP endif1;准备好展示
ATOF:加DL,55;转换10-15'A' - 'F'
endif1:INT 21H;显示字符
环FOR1;循环,直到DONE
;恢复寄存器
流行的DX
流行CX
流行斧
RET; dec_out将显示在BX寄存器中的二进制值作为基体10的值
dec_out:
;保存我们将要使用的寄存器
推斧
推BX
推CX
推DXXOR CX,CX; CX计数数字,最初为零
REPT:
MOV AX,BX;成立了由10除以
XOR DX,DX;必须有32位(无符号)股息
MOV BX,10;除数将在BX
DIV BX;商将在斧头,在剩余的DX
推DX;其余推栈上
INC CX;我们产生另一个数字,这样算什么呢
MOV BX,AX;商蜗居在BX
CMP AX,0;测试聪明的办法,如果商为0
JNE REPT;如果没有,生成一个数字MOV啊,2;显示字符功能
FOR2:;循环CX倍
流行的DX;流行的数字打印
或DL,30H;转换成数字打印到ASCII code
INT 21H;显示字符
环FOR2;并继续下去,直到所有的数字显示;恢复寄存器
流行的DX
流行CX
流行BX
流行斧
RET


解决方案

这是八进制数是一个二进制数的只是一个再presentation使它的更短,更说明。确切的3位被转换为一个八进制数字。

让我们用二进制数01101010做到这一点。

所有组首先在三胞胎(像一个十进制数)的数量:01101010 。第一组只有两个数字,所以加前导零:001101010。现在它转换为十进制:1,5,2。这些十进制数字是八进制数字为好,因此,八进制结果为152。

如果你有在计算机形式的一个号码,你可以通过改变或类似的直接访问二进制数字。

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

解决方案

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.

这篇关于转换二进制到八朗大会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆