簡體   English   中英

在匯編中使用ATOI將字符串轉換為整數時出現問題

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

我試圖讀取兩個字符串,使用atoi函數將它們轉換為數字,然后打印出結果。

這是我未初始化的變量。 (%定義BUFLEN 20)

SECTION .bss                    ; uninitialized data section

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

這是我獲取用戶輸入/嘗試將其分配到內存中的地方

   ; 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

現在我正在嘗試使用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

這是我的輸出...

輸入第一個#:1234

輸入秒#:1234

編號:1234

掛在第二個atoi電話上

首先,您沒有為輸入分配足夠的空間, 並且沒有正確讀取它。

如果輸入字符串12345678 ,則需要八個字節的字符,一個字節用於換行,一個字節用於終止\\0 因此, RESD 1不會減少芥末,它只會給您八個字節,而不是十個字節。

實際閱讀信息:

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是要讀取的字節數,由於某種原因將其設置為1 那不會得到您的完整數字,而只會得到第一個數字的第一個字符。

除了輸入問題之外,還有兩個問題。

首先,聲明:mov eax,[m]獲取m處的內存內容 如果您要撥打atoi ,它將需要該地址本身。

其次,您需要檢查您的呼叫約定。 esp增加價值似乎非常……對我而言很不尋常。 可能是正確的,但似乎與我見過的任何調用約定都不匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM