问题描述
我打算X变量转换为十进制。我使用的是涡轮汇编不好受,你可以给一个手?
I plan to convert the X variable to decimal. I'm having a hard time using turbo assembler, can you give a hand?
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
非常感谢
推荐答案
在将数字转换为可打印的格式通常最容易下手的最后一位。
When converting numbers to a printable format it's often easiest to start with the last digit.
考虑转换123123,怎么会得到最后的数字?它是由10(基数)除以时依然存在。所以123%10给我们3和十分之一百二十三= 12方便给了我们正确的号码与下一个迭代工作。在x86上的DIV指令是不够好,给我们俩商和余数(在斧
和 DX
分别)。剩下的就是可打印字符存储的字符串中。
Consider converting 123 to "123", how would we get the last digit? It's the remained when dividing by 10 (the base). So 123 % 10 gives us 3 and 123 / 10 = 12 conveniently gives us the correct number to work with in the next iteration. On x86 the "DIV" instruction is nice enough to give us both the quotient and remainder (in ax
and dx
respectively). All that remains is to store printable characters in the string.
把这个放在一起你结束了类似以下(使用NASM语法):
Putting all this together you end up with something like the following (using nasm syntax):
; 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
这需要一个工作缓存(为案件16时基= 2,为NUL终止一个额外的字节):
This requires a working buffer (16 for the case when base = 2 and an extra byte for the NUL terminator):
buffer: times 16 db 0
bufferend:
db 0
有符号数增加支持作为练习留给读者。 Here大致相同的程序适用于64位汇编。
Adding support for signed numbers is left as an exercise for the reader. Here is roughly the same routine adapted for 64-bit assembly.
这篇关于我怎么能转换成十六进制到十进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!