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
嘗試在strace('strace。/ a.out')下運行您的代碼,以查看實際傳遞給系統調用的參數。順便說一句,你的代碼對於一個問題來說相當長,它並不真正滿足[mcve]的最小部分。另請參閱:[x86標籤維基](http://stackoverflow.com/tags/x86/info)瞭解很多好東西。 –
首先,試着解釋'數字resb 1'的作用......以及'mov ecx,[digit]'做了什麼(這是無效的組合)。但是這部分意外地可能會工作(在代碼的最開始處執行'mov [digit],dword 0xDEADBEEF'以避免偶爾運行)。然後看看你如何使用'SYS_WRITE',當你顯示「msg」時,它是有效的,當你想要顯示總和時,你以不同的方式使用它(並且它不以那種方式工作)。仔細看看這些「int 80h」調用的參數。 – Ped7g