【问题标题】:Converting integer to senary[Base6] ASCII format将整数转换为 senary[Base6] ASCII 格式
【发布时间】:2025-12-20 02:05:17
【问题描述】:

我对如何实现这一点有疑问。我正在尝试将整数转换为 ASCII 格式的 senary 值。

;  Macro to convert integer to senary value in ASCII format.

;  Call:  int2senary    <integer>, <string-addr>
;   Arguments:
;       %1 -> <integer>, value
;       %2 -> <string>, string address

;  Reads <string>, place count including NULL into <count>
;  Note, should preserve any registers that the macro alters.
mov eax, %1 
mov r9d, 6 
convLoop: 
   div r9d 
   add edx, 48 
   push edx 
   cmp eax, r9d 
   jge convLoop 

【问题讨论】:

标签: assembly ascii x86-64 nasm


【解决方案1】:

关于这段代码的几点说明:

a 在执行DIV 之前,您需要清除EDX

b只要EAX不为0,迭代就必须继续。

c 你需要数一数你做了多少PUSH-es。在将结果存储在字符串中时,您如何知道稍后要执行多少 pop-s?

应用于代码:

  mov  eax, %1
  mov  r9d, 6
  xor  ecx, ecx     ; (c)
convLoop:
  xor  edx, edx     ; (a)
  div  r9d
  add  edx, 48
  push edx
  inc  ecx          ; (c)
  test eax, eax     ; (b)
  jnz  convLoop     ; (b)

【讨论】:

  • 这是 64 位代码。你不能push edx,只能dxrdx。通常你想要rdx,但每个数字 8 个字节很多。当然,如果您关心效率,则根本不会推送/弹出,而是递减一个指针并一次存储 1 个字节。 (然后,如果您需要将其与缓冲区的开头对齐,请使用 movups xmm0, [mem] 或其他内容将其移动到位。)