Monday, 15 August 2011

x86 - Fibonacci : Assembly language -



x86 - Fibonacci : Assembly language -

i'm trying simulate fibonacci sequence number given user. got code, asks one-digit input user. , accepts input 3 9.

.model little .stack 64 .data val1 db 01h val2 db 01h lp db 00h v1 db 00h v2 db 00h nl db 0dh,0ah,'$' .code main proc mov ax,@data mov ds,ax mov ah,01h int 21h mov cl,al sub cl,30h sub cl,2 mov ah,02h mov dl,val1 add together dl,30h int 21h mov ah,09h lea dx,nl int 21h mov ah,02h mov dl,val2 add together dl,30h int 21h mov ah,09h lea dx,nl int 21h disp: mov bl,val1 add together bl,val2 mov ah,00h mov al,bl mov lp,cl mov cl,10 div cl mov cl,lp mov v1,al mov v2,ah mov dl,v1 add together dl,30h mov ah,02h int 21h mov dl,v2 add together dl,30h mov ah,02h int 21h mov dl,val2 mov val1,dl mov val2,bl mov ah,09h lea dx,nl int 21h loop disp mov ah,4ch int 21h main endp end main

i tried modify it's going take two-digit input crashed. here's modified code:

.model little .stack 64 .data input db 13, 10, "input number of sequence: $" output db 13, 10, "the sequence $" val1 db 01h val2 db 01h lp db 00h v1 db 00h v2 db 00h nl db ' $' inp1a db ? inp1b db ? inputnum db ? .code main proc mov ax,@data mov ds,ax ;display input message lea dx, input mov ah, 09h int 21h ;gets tens digit of sequence number mov ah,01h int 21h sub al,'0' mov inp1b,al ; gets ones digit of sequence number mov ah,01h int 21h sub al,'0' mov inp1a,al ;makes 2 digits of sequence number whole number mov al, inp1b mov ch, 10 mul ch mov bl, inp1a add together bl, al mov inputnum, bl add together inputnum, '0' ;input --> cl mov cl, inputnum sub cl,30h sub cl,2 ;display output message lea dx, output mov ah, 09h int 21h ;display first fib number : 1 mov ah,02h mov dl,val1 add together dl,30h int 21h ;display space mov ah,09h lea dx,nl int 21h ;display sec fib number : 1 mov ah,02h mov dl,val2 add together dl,30h int 21h ;display space mov ah,09h lea dx,nl int 21h disp: ;adds 2 previous numbers mov bl,val1 add together bl,val2 ; new added number mov ah,00h mov al,bl ; new number ---> al mov cl,10 mov lp,cl ; lp have value of cl (inputnumber) div cl mov cl,lp mov v1,al ; v1 = new number (added) mov v2,ah ; v2 = null mov dl,v1 ; dl = new number add together dl,30h ; dl -> string mov ah,02h ; print value of v1 int 21h mov dl,v2 ; dl = v2 (null) add together dl,30h ; dl -> string mov ah,02h ; print value of v2 int 21h mov dl,val2 ; dl = new added number mov val1,dl ; val1 = dl = new added number mov val2,bl ; val2 = new added number = val2 ; display space mov ah,09h lea dx,nl int 21h loop disp mov ah,4ch int 21h main endp end main

is there solution work? in advance!

there lies 1 problem:

;makes 2 digits of sequence number whole number mov al, inp1b mov ch, 10 mul ch mov bl, inp1a add together bl, al mov inputnum, bl add together inputnum, '0'

the add together '0' used convert hex digit ascii representation ('0' 0x30, '1' 0x31, , on). if have 2 digits, number between 0 , 99. if it's greater 10, adding '0' gives wrong value. however, shouldn't create programme crash.

assembly x86 ms-dos fibonacci

No comments:

Post a Comment