MASM:整数字符串使用std和STOSB [英] MASM: integer to string using std and stosb

查看:265
本文介绍了MASM:整数字符串使用std和STOSB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户提供的整数转换为字符串以下过程。
我是pretty确保我的算法工作正常每个数字转换整数它是正确的十进制ASCII值。不过,我有困难,那么存储该数字将在outString的正确位置。

我一直使用STD设置方向标志,然后我用STOSB负载电流,转换成数字在outString(读从后到前)的最后一个字节。然而,这似乎并没有工作。

如果我摆脱'性病',我可以储存我的反向转换的整数。例如1993年获取存储在outString为'3991',但很明显,这是错误的。

应该AP preciate一些帮助。我使用的性病错了吗?
谢谢!

  writeVal PROC;设置堆栈帧
推EBP,EBP的旧值存储在系统堆栈
MOV EBP,尤指; EBP现在包含尤指价值,这是堆栈顶部的地址
MOV EDI,[EBP + 8]; EDI = @outStringSTD;设置方向标志。使得我们的字符添加到outString年底
MOV EAX,[EBP + 12]; EAX = userIntconvertInt:;该内部 - >字符串转换为一个检测后循环处理情况下用户输入0作为一个字符串    MOV EBX,10D; EBX = 10除数
    干熄焦; preP为师
    DIV EBX; EAX = QUOT,EDX =余    添加EDX,48;转换为EDX ASCII字符值    推EAX;存储在EAX电流值
    MOV EAX,EDX;设置EAX将转换ASCII值
    STOSB;存储转换的字符在outString年底    流行EAX; EAX恢复价值    调用WriteDec
    调用CRLF    CMP EAX,0
    JE完成;如果EAX = 0,这意味着我们已经完全转换userInt为字符串
    JMP convertInt;否则,重复完成:
    displayString [EBP + 8];打印转换outString    流行EBP
    RET 8
writeVal ENDP


解决方案

既然你有位数的变量,你这是怎么指定字符串的结尾。

  MOV EDI,[EBP + 8]; EDI = @outString
加入EDI,NumberOfDigits
十二月EDI;当DF = 1 STOSB从这里开始

I have the following procedure for converting a user supplied integer to a string. I'm pretty sure my algorithm is working fine for converting each digit of the integer to it's correct decimal ASCII value. However, I'm having difficulty then storing that digit into its correct place in the outString.

I've set the direction flag using 'std', and I then use stosb to load the current, converted digit into the last byte in outString (read from back to front). However, this does not seem to work.

If I get rid of 'std', I can store my converted integer in reverse. E.g 1993 gets stored in outString as '3991', but obviously that's in error.

Would appreciate some assistance. Am I using 'std' wrong? Thanks!

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 

解决方案

Since you have a variable for the number of digits, this is how you specify the end of the string.

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

这篇关于MASM:整数字符串使用std和STOSB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆