我计划将X变量转换为十进制.我很难使用turbo汇编程序,你能帮忙吗?
code segment ;inicio de un segmento unico
assume cs:code,ds:code,ss:code
org 100h ;localidad de inicio del contador
main proc ;procedimiento principal
mov ax,cs
mov ds,ax ; INICIO
mov ax, x
mov ah,4ch ;comienzo del fin de programa
int 21h ;fin del programa
main endp
x dw 0A92FH
code ends ; fin del segmento de codigo
end main ;fin del ensamble
Run Code Online (Sandbox Code Playgroud)
非常感谢
将数字转换为可打印格式时,通常最容易从最后一位开始.
考虑将123转换为"123",我们如何获得最后一位数?它除以10(基数)时保留.所以123%10给我们3和123/10 = 12方便地给我们在下一次迭代中使用的正确数字.在x86上,"DIV"指令很好地为我们提供了商和余数(分别在ax和中dx).剩下的就是在字符串中存储可打印的字符.
将所有这些放在一起你得到的结果如下(使用nasm语法):
; ConvertNumber
; Input:
; ax = Number to be converted
; bx = Base
;
; Output:
; si = Start of NUL-terminated buffer
; containing the converted number
; in ASCII represention.
ConvertNumber:
push ax ; Save modified registers
push bx
push dx
mov si, bufferend ; Start at the end
.convert:
xor dx, dx ; Clear dx for division
div bx ; Divide by base
add dl, '0' ; Convert to printable char
cmp dl, '9' ; Hex digit?
jbe .store ; No. Store it
add dl, 'A'-'0'-10 ; Adjust hex digit
.store:
dec si ; Move back one position
mov [si], dl ; Store converted digit
and ax, ax ; Division result 0?
jnz .convert ; No. Still digits to convert
pop dx ; Restore modified registers
pop bx
pop ax
ret
Run Code Online (Sandbox Code Playgroud)
这需要一个工作缓冲区(16表示base = 2时的情况和NUL终结符的额外字节):
buffer: times 16 db 0
bufferend:
db 0
Run Code Online (Sandbox Code Playgroud)
添加对签名号码的支持留给读者练习.这里大致相同的例程适用于64位汇编.