【问题标题】:MASM: integer to string using std and stosbMASM:使用 std 和 stosb 将整数转换为字符串
【发布时间】:2023-04-09 08:49:01
【问题描述】:

我有以下将用户提供的整数转换为字符串的过程。 我很确定我的算法可以很好地将整数的每个数字转换为正确的十进制 ASCII 值。但是,我很难将该数字存储到 outString 中的正确位置。

我已经使用“std”设置了方向标志,然后我使用 stosb 将当前转换后的数字加载到 outString 中的最后一个字节(从后向前读取)。但是,这似乎不起作用。

如果我去掉“std”,我可以反向存储转换后的整数。例如 1993 以 '3991' 的形式存储在 outString 中,但显然这是错误的。

不胜感激。我使用'std'错了吗? 谢谢!

writeVal PROC

; SET up the stack frame 
push    ebp     ;old value of ebp stored on the system stack
mov ebp, esp    ;ebp now contains value of esp, which is address of top of stack


mov edi, [ebp + 8]      ; EDI = @outString



std                 ; set direction flag. allows us to add chars to the end of outString


mov eax, [ebp + 12] ; EAX = userInt

convertInt:             ; the int->string conversion is a post-test loop to handle case where user input '0' as a string

    mov ebx, 10d            ; EBX = divisor of 10
    cdq                 ; prep for division
    div ebx             ; EAX = quot, EDX = remainder

    add edx, 48         ; convert EDX to ASCII char value

    push    eax             ; store current value in EAX
    mov eax, edx            ; set EAX to the converted ASCII value


    stosb               ; store the converted char at the end of outString

    pop eax             ;restore value of EAX

    call WriteDec
    call CrLf

    cmp eax, 0
    je  finished            ; if eax = 0, it means we have fully converted userInt to a string
    jmp convertInt      ; else, repeat

finished:
    displayString [ebp + 8] ; print the converted outString

    pop ebp
    ret 8
writeVal ENDP 

【问题讨论】:

  • 如果您向后存储,您可能需要从字符串的“末尾”开始存储。这可能会给您带来问题,因为在您知道所需的位数之前,您不知道从哪里开始,
  • @erikkallen 我确实有一个用于数字位数的变量。然后如何在代码中指定字符串的结尾?这是我想不通的。谢谢。

标签: string assembly x86 int masm


【解决方案1】:

由于您有一个数字位数的变量,这就是您指定字符串结尾的方式。

mov edi, [ebp + 8]      ; EDI = @outString
add edi, NumberOfDigits
dec edi                 ;When DF=1 STOSB starts here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多