2 notomorrow NoTomorrow_ 于 2016.04.02 18:24 提问

nasm提示非法指令,小白求解答 5C

刚学nasm,我捣鼓这个问题半天了,还是没找到bug,跪求解答,要用彩色输出fibonacci函数的值,输入用空格隔开如“12 13 14”,我用getint去获得输入的值,调用fibonacci1去计算,算出来的值用150位长的数组每个数组存一个字符,每次相加由arraybefore加到arraynow再由arraynow加到arraybefore里循环做加法,再用compare函数去选择颜色输出
代码如下
section .text
global main
;Declaring the external functions to be used in the program.......
getint: ;get the index of fibonacci
mov eax,3
mov ebx,0
mov ecx,tem1
mov edx,1
int 80h

mov eax,3
mov ebx,0
mov ecx,tem2
mov edx,1
int 80h
cmp word[tem2],32
je count1
cmp word[tem2],10
je count1

mov eax,3
mov ebx,0
mov ecx,tem3
mov edx,1
int 80h
cmp word[tem3],32
je count10
cmp word[tem3],10
je count10

count10:
sub word[tem1],30h
sub word[tem2],30h
mov ax,word[tem1]
mov dx,10
mul dx
add ax,word[tem2]
mov word[num],ax
call fibonacci1
call compare
cmp word[tem3],10
je exit
jmp getint
count1:
sub word[tem1],30h
mov ax,word[tem1]
mov word[num],ax
call fibonacci1
call compare
cmp word[tem2],10
je exit
jmp getint
ret

compare:
cmp byte[number],1
je print1
cmp byte[number],2
je print2
cmp byte[number],3
je print3
print1:
mov eax,4
mov ebx,1
mov ecx,echo1
mov edx,sizeEcho1
int 80h
add byte[number],1
jmp printfibo1
print2:
mov eax,4
mov ebx,1
mov ecx,echo2
mov edx,sizeEcho2
int 80h
add byte[number],1
jmp printfibo1
print3:
mov eax,4
mov ebx,1
mov ecx,echo3
mov edx,sizeEcho3
int 80h
mov byte[number],1
jmp printfibo1
ret

printfibo1:
pop edx
mov eax,arrayNow
cmp eax,edx
je end
mov eax,arrayBefore
cmp eax,edx
je end
mov al,byte[edx]
mov byte[fibo],al
sub edx,1
push edx
mov eax,4
mov ebx,1
mov ecx,fibo
mov edx,1
int 80h
jmp printfibo1
end:

mov eax,4
mov ebx,1
mov ecx,echoEnd
mov edx,sizeEchoEnd
int 80h
mov ecx,150
mov ebx,arrayNow
mov eax,arrayBefore
recover: ;recover array to init status
mov byte[ebx],-1
add ebx,1
mov byte[eax],-1
add eax,1
loop recover
ret

fibonacci1:
mov cx,word[num]
mov al,0
add al,30h ;integer 0 to char

mov byte[arrayBefore],al ;init arrayBefore[0] = '0'
mov al,1
add al,30h ;integer 1 to char
mov byte[arrayNow],al ;init arrayNow[0] = '1'
add:
cmp cx,0
je print
sub cx,1
cmp byte[record],0 ;the result of add to which array
je nowToBefore
jmp beforeToNow
nowToBefore:
mov ebx,arrayNow
mov edx,arrayBefore
mov byte[record],1
jmp addNumber
beforeToNow:
mov edx,arrayNow
mov ebx,arrayBefore
mov byte[record],0
jmp addNumber
addNumber: ;add index of i of two arrays[i]
mov al,byte[ebx]
cmp al,-1
je add ;jump to the next add
sub al,30h
mov ah,byte[edx]
cmp ah,-1

jne continue
mov ah,0
add ah,30h
continue:

sub ah,30h
add ah,al
add ah,byte[carry1]
cmp ah,10
jnb above10
mov byte[carry1],0
add ah,30h
mov byte[edx],ah
add ebx,1
add edx,1
jmp addNumber
above10:
mov byte[carry1],1
sub ah,10
add ah,30h
mov byte[edx],ah
add ebx,1
add edx,1
cmp byte[ebx],-1
je carry
jmp addNumber
carry:
mov ah,1
add ah,30h
mov byte[edx],ah
jmp add

print: push edx
ret

main:
;Main( ) Function
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, size1
int 80h
call getint

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

section .data
fmt: db "%d",0
msg1: db "Enter Fibonacci numbers:"
size1: equ $-msg1

number: db 0 ;which color to print
echo1: db 1Bh,"[48;32m"
sizeEcho1: equ $-echo1
echo2: db 1Bh,"[48;33m"
sizeEcho2: equ $-echo2
echo3: db 1Bh,"[48;34m"
sizeEcho3: equ $-echo3
echoEnd: db 1Bh,"[0m",10
sizeEchoEnd: equ $-echoEnd
arrayBefore: TIMES 150 db -1 ;init array of all -1
arrayNow: TIMES 150 db -1 ;the same as before
carry1: db 0 ;carry if the result of add two integers
record: db 0 ;record add arrayNow to arayBefore or opp

section .bss
num: resw 1 ;the index of fibonacci
tem1: resw 1
tem2: resw 1
tem3: resw 1
fibo: resb 1 ;as a byte to print

1个回答

devmiao
devmiao   Ds   Rxr 2016.04.03 03:58
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!