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

标签 assembly

我使用了一种简单的算法将数字转换为字符串,在 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/

相关文章:

c - gdb 跳转某些部分的汇编代码

assembly - 软件看门狗定时器复位 MPC875 CPU 过快

assembly - x86中的JCC指令是什么

function - 如果esp指向栈顶,那么ebp指向哪里?

assembly - imul 汇编指令 - 一个操作数?

assembly - .com 文件格式的详细信息是什么?

assembly - xor ax,加载​​段寄存器时的ax

c++ - vector 的值在不应该发生变化的时候发生了变化

assembly - Go 没有链接我的程序集 : undefined external function

assembly - 如何在程序集中获取用户输入