简体   繁体   English

在汇编中使用ATOI将字符串转换为整数时出现问题

[英]Trouble with converting string to integer using ATOI In Assembly

I'm trying to read in two strings, convert them into numbers using atoi function, and then print out the result. 我试图读取两个字符串,使用atoi函数将它们转换为数字,然后打印出结果。

Here's my uninitialized variables. 这是我未初始化的变量。 (%define BUFLEN 20) (%定义BUFLEN 20)

SECTION .bss                    ; uninitialized data section

m:           resb BUFLEN             ;STRING 1
mlen:        resb 4
r:           resb BUFLEN             ;STRING 2
rlen:        resb 4

Here's where I get the user input/ attempt allocate it into memory 这是我获取用户输入/尝试将其分配到内存中的地方

   ; prompt user for FIRST NUMBER

    mov     eax, SYSCALL_WRITE      ; write function
    mov     ebx, STDOUT             ; Arg1: file descriptor
    mov     ecx, msg1               ; Arg2: addr of message
    mov     edx, len1               ; Arg3: length of message
    int     080h                    ; ask kernel to write


    ; read in user input
    ;
    mov     eax, SYSCALL_READ       ; read function
    mov     ebx, STDIN              ; Arg 1: file descriptor
    mov     ecx, m                  ; Arg 2: address of buffer
    mov     edx, BUFLEN                  ; Arg 3: buffer length
    int     080h
    mov     [rlen], eax             ; save length of string read

    ; prompt user for SECOND NUMBER

    mov     eax, SYSCALL_WRITE      ; write function
    mov     ebx, STDOUT             ; Arg1: file descriptor
    mov     ecx, msg2               ; Arg2: addr of message
    mov     edx, len2               ; Arg3: length of message
    int     080h                    ; ask kernel to write

    ; read in user input
    mov     eax, SYSCALL_READ       ; read function
    mov     ebx, STDIN              ; source
    mov     ecx, r                  ; destination
    mov     edx, BUFLEN                  ; length of destination
    int     080h              
    mov     [mlen], eax             ; save length of string read

Now I'm trying to convert the strings using atoi and print them out 现在我正在尝试使用atoi转换字符串并将其打印出来

    ;CONVERT TO #
    mov     eax, 0                  ;zero out register
    mov     eax, m
    call    atoi
    add     esp, 4

    ;PRINT IT
    push    ax
    push    print_r
    call    printf
    add     esp, 8

    ;CONVERT TO #
    mov     eax, 0                  ;zero out register
    mov     eax, r
    call    atoi
    add     esp, 4

    ;PRINT IT
    push    ax
    push    print_r
    call    printf
    add     esp, 8

This is my output... 这是我的输出...

Enter first #: 1234 输入第一个#:1234

Enter second#: 1234 输入秒#:1234

Number: 1234 编号:1234

Hanging on second atoi call 挂在第二个atoi电话上

For a start, you're not allocating enough space for the input and you're not reading it properly. 首先,您没有为输入分配足够的空间, 并且没有正确读取它。

If you input the string 12345678 , you need eight bytes for the characters, one for the newline, and one for the terminating \\0 . 如果输入字符串12345678 ,则需要八个字节的字符,一个字节用于换行,一个字节用于终止\\0 So, a RESD 1 is not going to cut the mustard, it only gives you eight bytes rather than ten. 因此, RESD 1不会减少芥末,它只会给您八个字节,而不是十个字节。

For actually reading the information: 实际阅读信息:

mov     eax, SYSCALL_READ       ; read function
mov     ebx, STDIN              ; Arg 1: file descriptor
mov     ecx, m                  ; Arg 2: address of buffer
mov     edx, 1                  ; Arg 3: buffer length
int     080h

edx is meant to be the number of bytes to read and you have set it to 1 for some reason. edx是要读取的字节数,由于某种原因将其设置为1 That's not going to get your entire number, rather it will just get the first character of the first number. 那不会得到您的完整数字,而只会得到第一个数字的第一个字符。

On top of the input problems, there's a couple of problems there as well. 除了输入问题之外,还有两个问题。

First, the statement: mov eax, [m] gets the contents of memory at m . 首先,声明:mov eax,[m]获取m处的内存内容 If you're calling atoi , it will want the address itself. 如果您要拨打atoi ,它将需要该地址本身。

Secondly, you need to examine your calling convention. 其次,您需要检查您的呼叫约定。 The adding of values to esp seems very ... unusual to me. esp增加价值似乎非常……对我而言很不寻常。 It may be correct but it doesn't seem to match any calling convention I've ever seen. 可能是正确的,但似乎与我见过的任何调用约定都不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM