装配中的基本转换说明

时间:2015-08-23 13:04:05

标签: assembly base

我真的需要对这些标签中给出的代码做进一步的解释:

ACCEPT,INC_CTR,EXIT,BIN_ACCEPT,DISP_ZERO,
HEX_ACCEPT,LETTERS,SECOND_DIGIT, CONVERTTOOCTAL

我需要这个,以便进一步了解输入如何转换为几个基础。我确实理解数据定义和代码,但整个过程太复杂了。

.model small
.stack 90h   

.data 
counter db 0
curValue db 0
prevValue db 0 
hexa db 0
octal db 0 
msg db "Enter a decimal number: $"
msg2 db "In binary: $"
octmsg db "In octall: $"
hexmsg db "In hexadecimal: $"

.code
mov ax, @data           ;initialize DS
mov ds, ax 

;mov counter, 0              ;load and display the string msg
mov ah, 09h
lea dx, msg         
int 21h

accept: 
mov ah, 01          
int 21h                 ;read a digit

cmp al, 13              ;compare al with 13
je exit                 ;jump to label exit if input is 13 

sub al, 48              ;subract al with 48
mov curValue, al         ;move al to curValue

cmp counter, 1              ;compare counter with 1
jl inc_ctr            ;jump to label inc_ctr if al<1

mov al, prevValue        ;move prevValue to al
mov bl, 10
mul bl

add al, curValue         ;add curValue to al

mov prevValue, al        ;move al tp prevValue

inc counter                 ;inc_ctr counter
jmp accept              ;jump to label accept

inc_ctr:
mov prevValue, al        ;move al to prevValue 

inc counter                 ;inc_ctr counter
jmp accept              ;jump to label accept

exit:
mov bl,prevValue         ;move prevValue to bl
mov hexa, bl            ;move bl to hexa
mov bl, 0               ;move 0 to bl

mov ah, 02h             ;set output function
mov dl, 13              ;set dl to 13
int 21h                 ;print the character

mov ah, 02h             ;set output function
mov dl, 10              ;set dl to 10
int 21h                 ;print character

mov ah, 09h             ;load and display the string msg2
lea dx, msg2     
int 21h

binAccept:
cmp bl, 8               ;compare bl with 8
jge hexAccept          ;jump to hexAccept if bl>=0
rol prevValue, 1         ;rotate prevValue 1x
jnc dispZero
mov ah, 02              ;set output function
mov dl, '1'             ;display 1
int 21h                 ;print the character

inc bl
jmp binAccept        ;jump to label binAccept

dispZero:
mov ah, 02h             ;set the output function
mov dl, '0'             ;display 0
int 21h                 ;print the character

inc bl                  ;inc_ctr bl
jmp binAccept        ;jump to label binary accept

hexAccept: 
mov ah, 02h             ;set output function
mov dl, 13              ;set dl to 13
int 21h                 ;print the character

mov ah, 02h             ;set the output function
mov dl, 10              ;set dl to 10
int 21h                 ;print the character

mov ah, 09h             ;load and display the string hexmsg
lea dx, hexmsg
int 21h 

mov bl,hexa             ;move hexa to bl
mov octal, bl            ;move bl to octal
xor bx, bx             ;clear bx

mov bh, 240             ;move 240 to bh
and bh, hexa            ;multiply hexa with bh
mov bl, 15              ;move 15 to bl 
and bl, hexa            ;multiply hexa with bl

mov cl, 4               ;move 4 to cl
rol bh, cl              ;rotate bh 4x

cmp bh, 9               ;compare bh with 9
jg Letters        ;jump to Letters if bh>9
add bh, 48              ;add 48 to bh
mov ah, 02h             ;set the output function
mov dl, bh              ;move bh to dl
int 21h                 ;print the character
jmp Second_digit     ;jump to Second_digit

Letters:
add bh, 55              ;add 55 to bh

mov ah, 02h             ;set the output function
mov dl, bh              ;move bh to dl
int 21h                 ;print the character

Second_digit:       
cmp bl, 9               ;compare bl with 9
jg dispSecond_digit       ;jump to dispSecond_digit if bl>9
add bl, 48              ;add 48 to bl
mov ah, 02h             ;set the outputfunction
mov dl, bl              ;move bl to dl
int 21h                 ;print the character
jmp convertTooctall      ;jump to convertTooctall

dispSecond_digit:
add bl, 55              ;add 55 ot bl
mov ah, 02h             ;set the output function
mov dl, bl              ;move bl to dl
int 21h                 ;print the character

convertTooctall:
mov ah, 02h             ;set the output function
mov dl, 13              ;move 13 to dl
int 21h                 ;print the charater

mov ah, 02h             ;set the output function
mov dl, 10              ;move 10 to dl
int 21h                 ;print the character

mov ah, 09h             ;load and display the string ctmsg
lea dx, octmsg
int 21h 

mov bh, octal            ;move octal to bh

and bh, 192             ;multiply 192 to bh          
mov cl, 2               ;move 2 to cl
rol bh, cl              ;rotate bh 2x

add bh, 48              ;add 48 to bh 
mov ah, 02              ;set the output function
mov dl, bh              ;move bh to dl
int 21h                 ;print the character

mov bh, octal            ;move octal to bh
and bh, 56              ;add 56 to bh
mov cl, 5               ;move 5 to cl
rol bh, cl              ;rotate bh 5x
add bh, 48              ;add 48 to bh
mov ah, 02              ;set the output function
mov dl, bh              ;move bh to dl
int 21h                 ;print the character

mov bh, octal            ;move octal to bh
and bh, 7               ;mulptiply by 7

add bh, 48              ;add 48 to bh
mov ah, 02              ;set the output function
mov dl, bh              ;move bh to dl
int 21h                 ;print the character

mov ah, 04ch            ;return control to DOS
int 21h 

1 个答案:

答案 0 :(得分:0)

代码使用特殊情况和技巧,但在场景后面最终与此相同:

从二进制(机器代码)到二进制,八进制,十六进制(十进制或任何基数)的人类可读/可打印ASCII的基本转换由以下内容完成:

  1. 如果数字为零,则放置&#34; 0&#34;在缓冲区并转到第7步
  2. 将基数设置为希望输出模式;例如2,8,16或10
  3. 将数字除以基数,保留余数并将商存回作为下一次迭代的数字。
  4. 将余数用作带有字符的表格的索引&#34; 0123456789ABCDEF&#34;并选择放置角色
  5. 将字符前缀到缓冲区
  6. 如果(步骤3)不为零,请返回步骤3
  7. 退出:此时缓冲区保存输出字符串。
  8. 现在注意除非你有一个除法指令(除了这个简短的文字),除以10在汇编语言中有点棘手。 除以2,4,8或16等于向右移位一位,两位,三位或四位(=除以2 ^ n,其中n = 1,2,3或4)。对于这些,剩余部分可以在分割之前选择 - 在&#34;和#34; -instruction中使用适当的掩码。

    负数:检测它,为待定的&#34;减号设置标志&#34;要在退出前添加到缓冲区,否定数字并使用上面的算法,记得在退出/显示之前添加减号。