【发布时间】:2018-03-11 11:11:38
【问题描述】:
在将十进制值转换为二进制时,我无法分析这段代码。有人可以帮助解释下面的文字行吗?顺便说一句,这是一个工作代码。我非常感谢您的帮助,这是出于学习目的。谢谢你!
calc :
mul multiplier
mov bl, byte ptr [si]
mov bh, 00h
add ax, bx
inc si
loop calc
mov si, offset buf4 + 2
mov bx, ax
mov dx, 0000h
mov ax, 8000h
convert :
mov cx, 0000h
conv :
cmp bx, ax
jb cont3
sub bx, ax
inc cx
jmp conv
cont3 :
add cl, 30h
mov byte ptr [si], cl
inc si
mov cx, 0002h
div cx
cmp ax, 0000h
jnz convert
mov byte ptr [si], '$'
prnstr buf3
prnstr buf4+2
stop :
mov ax, 4c00h
int 21h
代码的第一部分很容易理解(接受用户输入并检查其是否为有效的十进制数)。我已经添加了一些 cmets.. 代码的前半部分如下所示:
prnstr macro msg ;acts like a function to print string
mov ah, 09h
lea dx, msg
int 21h
endm
data segment
buf1 db "Enter a decimal number : $"
buf2 db 0ah, "Invalid Decimal Number...$"
buf3 db 0ah, "Equivalent Binary number is : $"
buf4 db 6
db 0
db 6 dup(0)
multiplier db 0ah
data ends
code segment
assume cs:code, ds:data
start :
mov ax,@data
mov ds,ax
mov es, ax
prnstr buf1 ;Display
mov ah, 0ah ;Get line function
lea dx, buf4 ;adress of input buffer
int 21h
mov si, offset buf1 + 2 ;Load pointer to beginning of structure
mov cl, byte ptr [si-1] ;determine end of loop?
mov ch, 00h
subtract :
mov al, byte ptr [si] ;Load 1 byte of buf1 to cl
cmp al, 30h ;check if not below 0
jnb cont1
jmp stop
cont1 :
cmp al, 3ah ;check if not above 9
jb cont2
prnstr buf2 ;Display
jmp stop
cont2 :
sub al, 30h ;convert ASCII to Decimal number
mov byte ptr [si], al ;place bcd form back to pointed character
prnstr buf2 ;Display
inc si ;next character
loop subtract ;repeat until end of string
mov si, offset buf1 + 2 ;Load pointer to beginning of structure
mov cl, byte ptr [si-1] ;reset to determine end of loop using si
mov ch, 00h
mov ax, 0000h ;reset ax to 0
;...CONTINUE TO calc.....
【问题讨论】:
标签: assembly x86 dos masm x86-16