Created
November 17, 2017 22:49
-
-
Save epk/fd6411f89b8fd298cdeebb9c79017df7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
.186 ;You may need to uncomment this to get some of the instructions working (e.g. shl, dest, count) | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
data segment ; data segment. Keyword db means define byte. You can also define word (dw) | |
n dw 5 ;nth fib. number | |
firstS db 'The Fibonacci number of order $' ;First part of output string | |
secS db ' is $' ;Second part of output string | |
newL db 0ah,'$' ;New line character | |
temp db 1 dup(?),'$' ;Used for output | |
data ends | |
; stack segment | |
stack1 segment stack | |
db 100 dup(?) ; This is the stack of 100 bytes | |
stack1 ends | |
code segment | |
assume cs:code, ds:data, ss:stack1 | |
start: | |
;Perform initialization | |
mov ax, data ;Put the starting address of the data segment into the ax register (must do this first) | |
mov ds, ax ;Put the starting address of the data segment into the ds register (where it belongs) | |
mov ax, stack1 ;Put the starting address of the stack into the ax register (must do this first) | |
mov ss, ax ;Put the starting address of the stack segment into the ss register (where it belongs) | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
;****************** Write Code Here ****************** | |
mov cx, n ;Move n into cx (counter for the loop) | |
mov bl, 0 ;Initialize bl to 0 (used to store result) | |
mov dl, 0 ;Store current fib. # (i.e. 0 is the 0th fib. #) | |
call pEvery ;Output Fibonacci number | |
cmp cx, 0 ;Check to see if cx is 0 | |
je fEnd ;Jump to end if cx == 0 | |
mov al, 0 ;Initialize al to 0 | |
mov bl, 1 ;Initialize bl to 1 | |
inc dl ;Increment dl to indicate the current fib. # | |
dec cx ;Decrement cx for algorithm | |
call pEvery ;Output Fibonacci numbers | |
jz fEnd | |
loopS: ;Loop label | |
add bl, al ;Add al and bl and store result in bl (nth fib. #) | |
dec cx ;Decrement counter | |
inc dl ;Increment dl to indicate current fib. # | |
call pEvery ;Output Fibonacci number | |
cmp cx, 0 ;Check if finished processing | |
jg loopS ;Continue while cx>0 | |
fEnd: | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
mov ah, 4ch ;Set up code to specify return to dos | |
int 21h ;Interpt number 21 (Return control to dos prompt) | |
pEvery proc | |
push ax ;Back up ax | |
push dx ;Back up dx | |
mov temp, dl ;Put dl into memory for output | |
add temp, 30h ;Convert dl into character | |
mov ah, 9 ;Specify print string service routine | |
lea dx, firstS ;Put the first part of output string into dx | |
int 21h ;Request service from the OS | |
lea dx, temp ;Put address of current number of fib. into dx | |
int 21h ;Request service from the OS | |
lea dx, secS ;Put the second part of output string into dx | |
int 21h ;Request service from the OS | |
mov temp, bl ;Put current fib. # into memory | |
add temp, 30h ;Convert it to a character | |
lea dx, temp ;Put address of current fib. number into dx | |
int 21h ;Request service from the OS | |
lea dx, newL ;Put address for new line string into dx | |
int 21h ;Request service from the OS | |
pop dx ;Restore dx | |
pop ax ;Restore the value of ax | |
ret | |
pEvery endp | |
;***************************************************** | |
code ends | |
end start | |
;------------------------------------------------------------------------------------------------------------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment