编写并运行一个程序,添加5字节的数据并保存结果



编写并运行一个程序,添加5字节的数据并保存结果。数据应该是如下十六进制数:25、12、15、IF和2B。显示程序和输出的快照。程序的开头是:

.MODEL SMALL  
.STACK 0100h  
.DATA  
DATA_IN DB 25H, 12H, 15H, 1FH, 2BH 
sum db?

我无法得到十六进制的输出。我已经尝试过这个代码,但仍然不能得到我想要的输出:

.model small
.stack 100h
.data
data_in db 25H, 12H, 15H, 1FH, 2BH 
sum db ?
msg1    db 'The result is : $' ;
.code
mov ax,@data
mov ds,ax
lea si,data_in
mov al,[si]
mov cx, 4
again:
inc si
add al, [si]
loop again
mov sum,al

lea dx, msg1
mov ah,09h
int 21h
mov ah, 2
mov dl, SUM
int 21h
mov ah,4ch ; end operation
int 21h
end

打印十六进制字节的关键是记住数字的ASCII码是它的值加上0x30。这段代码有很多版本比这段代码优化得多,但更难阅读和理解,所以我给你一个更容易读的版本:

PrintHex:
;input: al = the byte you wish to print.
push ax
shr al,1
shr al,1
shr al,1
shr al,1     ;move the top 4 bits to the bottom.
call ShowHex
pop ax
and al,0Fh   ;now print the bottom 4 bits as ASCII.
;fallthrough is intentional - we want to run this code again and return.
ShowHex:
add al,30h       ;converts the digit to ASCII
cmp al,3Ah       ;if greater than A, we need letters A through F
jc noCorrectHex
add al,7     ;3Ah becomes "A", 3Bh becomes "B", etc.
noCorrectHex:
mov dl,al
mov ah,2
int 21h      ;print
ret

最新更新