2013-08-12 37 views
1

我遇到了執行結果問題。如果你追蹤它,這似乎是正確的。 但是,它給出了錯誤的結果。 如果你輸入99和99這給了5318,但它應該給9801.通過在nasm中重複添加堆棧進行乘法運算

順便說一句,該程序通過將被乘數(第一輸入)接受兩個2位數字和乘法,直到它在乘數滿足值(第二個輸入)

我試圖找到問題,似乎當我使用字大小的分裂它給隨機值。字大小高達65,656,但結果超過255時會破壞。爲什麼?

請注意第一個選項是重複加法運算的乘法運算。

有一點幫助是非常感謝。如下圖所示

section .data 
msg db "Menu: " 
msgLen equ $ -msg 
msg2 db "[1]Multiplication by repeated addition" 
msgLen2 equ $ -msg2 
msg3 db "[2]Division by repeated subtraction" 
msgLen3 equ $ -msg3 
msg4 db "[3]Exit" 
msgLen4 equ $ -msg4 
msg5 db "Enter two numbers: " 
msgLen5 equ $ -msg5 
line db "", 10 
result dw 0 
ten dw 10 
quo1 dw 0 
quo2 dw 0 
quo3 dw 0 
rem1 dw 0 
rem2 dw 0 
rem3 dw 0 
temp1 db 0 
temp2 db 0 
temp3 db 0 

    section .bss 
choice resb 1 
num1a resw 1 
num1b resw 1 
num2a resw 1 
num2b resw 1 


    section .text 
global _start 

    _start: 
    do_while: 
mov eax, 4 
mov ebx, 1 
mov ecx, msg 
mov edx, msgLen 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, line 
mov edx, 1 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, msg2 
mov edx, msgLen2 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, line 
mov edx, 1 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, msg3 
mov edx, msgLen3 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, line 
mov edx, 1 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, msg4 
mov edx, msgLen4 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, line 
mov edx, 1 
int 80h 

mov eax, 3 
mov ebx, 0 
mov ecx, choice 
mov edx, 2 
int 80h 

sub byte [choice], 30h 

cmp byte [choice], 1 
je menu1 
;cmp byte [choice], 2 
;je menu2 
cmp byte [choice], 3 
je exit 
jg do_while 
jl do_while 

    menu1: 
mov eax, 4 
mov ebx, 1 
mov ecx, msg5 
mov edx, msgLen5 
int 80h 

mov eax, 3 
mov ebx, 0 
mov ecx, num1a 
mov edx, 1 
int 80h 

mov eax, 3 
mov ebx, 0 
mov ecx, num1b 
mov edx, 2 
int 80h 

mov eax, 3 
mov ebx, 0 
mov ecx, num2a 
mov edx, 1 
int 80h 

mov eax, 3 
mov ebx, 0 
mov ecx, num2b 
mov edx, 2 
int 80h 

sub word [num1a], 30h  ;conversion 
sub word [num1b], 30h 
sub word [num2a], 30h 
sub word [num2b], 30h 

push result 
push word [num1a] 
push word [num1b] 
push word [num2a] 
push word [num2b] 
call func1 

mov ax, [result]    ;9801 
mov dx, 0 
mov bx, 10 
div bx 

mov word [quo1], ax    ;980 
mov word [rem1], dx    ;1 

mov ax, [quo1]    
mov dx, 0 
mov bx, 10 
div bx 

mov word [quo2], ax    ;98 
mov word [rem2], dx    ;0 

mov ax, [quo2] 
mov dx, 0 
mov bx, 10 
div bx 

mov word [quo3], ax    ;9 
mov word [rem3], dx    ;8 


add word [quo3], 30h 
add word [rem3], 30h 
add word [rem2], 30h 
add word [rem1], 30h 

mov eax, 4 
mov ebx, 1 
mov ecx, quo3 
mov edx, 1 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, rem3 
mov edx, 1 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, rem2 
mov edx, 1 
int 80h 

mov eax, 4 
mov ebx, 1 
mov ecx, rem1 
mov edx, 1 
int 80h 



mov eax, 4 
mov ebx, 1 
mov ecx, line 
mov edx, 1 
int 80h 

jmp do_while 

    func1: 
mov ebp, esp 

mov ax, [ebp+10]  ;90 
mov dx, 0 
mul word [ten] 
mov [ebp+10], ax 

mov ax, [ebp+8]   ;9 
add word [ebp+10], ax   ;99 on [ebp+10] 

mov ax, [ebp+6]   ;90 
mov dx, 0 
mul word [ten] 
mov [ebp+6], ax 

mov ax, [ebp+4]   ;9 
add word [ebp+6], ax   ;99 on [ebp+6] 

    while1: 
mov ax, [ebp+10] 
add ax, [ebp+10] 

dec word [ebp+6] 
cmp word [ebp+6], 1 
jne while1 

    end1:          
mov ebx, [ebp+12] 
mov [ebx], ax 
ret 12 

    exit: 
mov eax, 1 
mov ebx, 0 
int 80h 
+0

你確定'numXY'變量的高字節是0嗎? – Michael

+0

什麼numXY?對不起,你可以澄清。 – ThisGuy

+0

XY = 1a,1b等 – Michael

回答

0

你的循環是不正確的,因爲它在每次迭代的開始重置的ax值。所以在退出循環時,無論迭代次數如何,ax都將包含原始值[ebp+10]乘以二。

while1: 
    mov ax, [ebp+10] 
    add ax, [ebp+10] 

    dec word [ebp+6] 
    cmp word [ebp+6], 1 
    jne while1 

好吧,讓我們來看看你的5318.推測的結果,這就是[ebp+10]值的兩倍;因此[ebp+10]的值將爲2659,即十六進制的0x0A63。這表明num1bnum2b的高字節包含一個換行符(ASCII代碼0x0A)。

所以你需要修復你的循環。像這樣的東西應該工作:

mov ax, 0 
while1: 
    add ax, [ebp+10] 

而且你還需要明確的num1a..num2b高字節。

+0

當我打印它們時,num1a,num1b,num2a,num2b保留9。我還打印了[ebp + 10]和[ebp + 6],他們都給了99.我自己添加了[ebp + 10]一次,並且它使用字節大小劃分產生了198個字符,但字符大小沒有。我很困惑。 – ThisGuy

+0

你是什麼意思的高字節? – ThisGuy

+0

一個字有16位。由高位字節I表示位8..15。 – Michael