2016-10-13 62 views
0

我在Linux VM上使用x86彙編代碼intel格式與NASM。 該程序應該佔用空格分隔兩位數字並打印出總和。我已經用GDB來看它,它在整個過程中都能正常工作,沒有任何錯誤或者任何其他問題,除了它無聲地拒絕打印結果。我是新來的彙編代碼,所以我不知道我在這裏做錯了什麼。彙編程序缺少輸出

編輯:縮短代碼只包含更多相關位。我認爲。

;Variables 
section .bss 
    digit  resb 1   

_start: 
    ;Input Prompt 
     ;Code block edited out. 

    ;Reads 1st digit input, checks if the read operation was successfull, 
    ;and stores the value in EAX. 
    call _readDigit 
    cmp edx,0 
    jne _end 
    mov eax,ecx 

    ;Reads 2nd digit input, checks for read success, 
    ;and stores the value in EBX. 
    call _readDigit 
    cmp edx,0 
    jne _end 
    mov ebx,ecx 

    ;Sums EAX and EBX, stores result in ECX, 
    ;and calls the write procedure. 
    call _newLine 
    add eax,ebx 
    mov ecx,eax 
    call _writeSum  

;Prints out the sum of two digits in the format 0X for values 
;below 10, or 1X for values greater than 9. 
_writeSum: 
    push eax 
    push ebx 
    push ecx 
    push edx 

    mov [digit],ecx 
    cmp ecx,9  ;Checks if sum > 9. 
    jg _twoDigits 

;Prints out 0 for the first digit in the result. 
_oneDigit: 
    mov ecx,48 
    mov edx,1 
    mov ebx,STDOUT 
    mov eax,SYS_WRITE 
    int 80h 

    mov ecx,[digit] 
    jmp _lastDigit 

;Prints out 1 for the first digit in the result, 
;and subtracts 10 from ECX. 
_twoDigits: 
    mov ecx,49 
    mov edx,1 
    mov ebx,STDOUT 
    mov eax,SYS_WRITE 
    int 80h 

    mov ecx,[digit] 
    sub ecx,10 

;Converts ECX to ASCII and prints this as the 
;second digit in the result. 
_lastDigit: 
    add ecx,'0' 
    mov [digit],ecx 

    mov ecx,[digit] 
    mov edx,1 
    mov ebx,STDOUT 
    mov eax,SYS_WRITE 
    int 80h 

    pop edx 
    pop ecx 
    pop ebx 
    pop eax 
    ret 
+0

嘗試在strace('strace。/ a.out')下運行您的代碼,以查看實際傳遞給系統調用的參數。順便說一句,你的代碼對於一個問題來說相當長,它並不真正滿足[mcve]的最小部分。另請參閱:[x86標籤維基](http://stackoverflow.com/tags/x86/info)瞭解很多好東西。 –

+1

首先,試着解釋'數字resb 1'的作用......以及'mov ecx,[digit]'做了什麼(這是無效的組合)。但是這部分意外地可能會工作(在代碼的最開始處執行'mov [digit],dword 0xDEADBEEF'以避免偶爾運行)。然後看看你如何使用'SYS_WRITE',當你顯示「msg」時,它是有效的,當你想要顯示總和時,你以不同的方式使用它(並且它不以那種方式工作)。仔細看看這些「int 80h」調用的參數。 – Ped7g

回答

0

我馬上看到的一件事就是您將要打印的值(48,49,[digit])直接轉移到ecx中。 SYS_WRITE期望ecx將一個POINTER包含到文本字符串中,而不是直接包含該值。其次,你應該考慮用你期望的值手動填充字符串(真的是所有的輸入),這樣你就可以確定問題不在讀取等等。簡單地忽略被調用的代碼doesn'讓你得到一個最小的錯誤情況,它只是做出大部分時間都是正確的假設。