我下面的代碼,出于某種原因只輸出數字的最后 2 位數字,無論數字有多長......有人可以告訴我問題/如何解決嗎?當然,我想列印出整個數字,但它不允許我這樣做。請幫助,謝謝
global _start
section .text
_start:
mov eax, 6789 ; The number to be printed
mov ebx, 0 ; count the digits
mov ecx, 10 ; divisor
divide:
mov edx, 0
div ecx ; divide the number into eax and edx | for 6789, eax = 678, edx = 9
push ebx ; push current count into the stack
inc ebx ; increase the number of digits
push edx ; push the value of remainder into the stack
cmp eax, 0 ; check if eax = 0, means all digits are pushed into the stack
jne divide
; Stack at the end of divide: (entry from left): 6, 3, 7, 2, 8, 1, 9, 0 ]
print:
pop edx ; take out the value pushed previously in the stack
add edx, 0x30 ; Convert to ASCII
mov [digit], edx ; Save the value of edx to digit
mov eax, 4 ;\
mov ebx, 1 ; |---> use sys_write to print on screen
mov ecx, digit ; |
mov edx, 1 ;/
int 0x80
pop ebx ; Restore value of count to ebx from the stack
cmp ebx, 0 ; compare whether count is equal to 0
jnz print ; if not 0, go back to print and print the next digit on the screen
concl:
mov eax, 1
mov ebx, 0
int 0x80
section .data
digit db 0
我使用 gdb-(GEF) 進行除錯,堆疊似乎運行正常,所有暫存器也一樣。
示例輸出為數字:6789 輸出為:89
uj5u.com熱心網友回復:
一個糟糕的續行案例
mov [digit], edx ; Save the value of edx to digit mov eax, 4 ;\ mov ebx, 1 ; |---> use sys_write to print on screen mov ecx, digit ; | mov edx, 1 ;/
不要無意中在一行的末尾放置一個反斜杠字符,因為匯編程式會將其視為行繼續的信號,這意味著后面的行將縫合到當前行,在您的情況下,它會產生以下內容:
mov [digit], edx ; Save the value of edx to digit
mov eax, 4 ; mov ebx, 1 ; |---> use sys_write to print on screen
mov ecx, digit ; |
mov edx, 1 ;/
加載 EBX 并參考 STDOUT 的部分現在位于分號后面,并成為程式中的一個注釋。您不會在可執行檔案中找到它。列印回圈將使用 EBX 中的任何內容。
在編號為 6789 的測驗運行中,列印回圈的前 2 次迭代使用 EBX=4 和 EBX=3,但最后 2 次迭代使用 EBX=2 和 EBX=1。現在 STDERR=2 和 STDOUT=1,并且都寫入螢屏。這就是為什么你只得到最后 2 位數字的輸出。
盡管您應該撰寫mov [digit], dl而不是mov [digit], edx,但代碼似乎是正確的。您對 double 的不尋常選擇與 doublepush相匹配pop,所以這很好。你只需要洗掉那個倒霉的\
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533855.html
標籤:Intel Collective 部件x86鼻炎32位
上一篇:無法理解此匯編代碼
