gpt4 book ai didi

assembly - 将转换后的数字附加到现有字符串

转载 作者:行者123 更新时间:2023-12-01 09:53:00 24 4
gpt4 key购买 nike

我使用了一种简单的算法将数字转换为字符串,在 SO 上找到,我已根据自己的目的对其进行了调整,但它并不能完全满足我的需要。我有一个字符串:

string point, "xxx points"

和常量:

const10    dd 10
points dd 50 ; range from 0 - 990

调整后的行程

mov eax, [points] ; number to be converted
.convNum:
push eax
push edx
xor edx,edx
div dword [const10] ;eax = result, edx = remainder
cmp eax, dword 0 ; test of the res
je .print
jmp .convNum
.print:
lea eax,[edx+'0']
mov [point], dword eax ; need to replace xxx in string by number
pop edx
pop eax

这个没有按预期工作,因为它只显示一位数字,我理解这一点,但我无法以我想要的方式更改例程。
编辑:
添加另一个版本的代码

push eax
push edx
mov eax, [points]
.convNum:
xor edx,edx
div dword [const10]
lea eax,[edx+'0']
mov ecx,[counter]
cmp ecx, 1
jne .next
mov [point+2], byte al
mov eax,[points]
div dword [const10]
jmp .again
.next:
cmp ecx, 2
jne .next2
mov [point+1], byte al
mov eax,[points]
div dword [const10]
div dword [const10]
jmp .again
.next2:
mov ecx,[p_bodu]
cmp ecx, 100
jb .kend
mov [point], byte al
jmp .kend
.again:
inc ecx
mov [counter],ecx
jmp .convNum
.kend:
pop edx
pop eax

此代码采用从低到高的数字。像 120 这样的数字转换得很好,但是像 130,150 这样的数字转换成 630、650,同样数字 140 转换得很好

最佳答案

你一直在循环中插入 EAX 和 EDX。你只需要这样做一次。

.convNum 标签向下移动 2 行:

   mov eax, [points] ; number to be converted
push eax
push edx
.convNum:
xor edx,edx
div dword [const10] ;eax = result, edx = remainder
cmp eax, dword 0 ; test of the res
jne .convNum
.print:

另请注意,您应该写一个字节而不是一个双字:

mov [point], al ;

您编写的代码只会处理所提供值的最高位。要继续检索位置较低的数字,您需要从数字中减去当前数字乘以(10 的倍数)并重复代码。

关于assembly - 将转换后的数字附加到现有字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34199621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com