Skip to content

Instantly share code, notes, and snippets.

@tatey
Created July 21, 2009 12:13
Show Gist options
  • Save tatey/151309 to your computer and use it in GitHub Desktop.
Save tatey/151309 to your computer and use it in GitHub Desktop.
Factorial in assembly (M6800)
; Course: 1007ICT - Griffith University
; Assignment: 2
; Author: Tate Johnson
; Created: 2008-05-30
; Processor: Motorola 6800
; QUESTION 6
; Calculate factorial of a
; number ranging from 1 - 8,
; depending on desired value
; inputted by user on their
; keyboard
; Store a 16 bit result as a
; high byte and low byte.
; ByteHi $0120 and ByteLo $0121
; ### START OF PROGRAM ###
; To save resources, we'll check
; user input with FactorMin to
; calculate 1! = 1
FactorMin .equ 1
; KeyInput will setup X register
; to correctly capture user
; input from keyboard
KeyInput .equ $FFFF
clra
clrb
; Wait for user input from
; keyboard interrupt
wai
ldx #KeyInput
ldaa 0,x
; Decode ASCII value in to
; desired factorial
ldab #48
sba
; If input is 0, save resources
; and skip to end of program
tsta
beq SkipToEnd
; If input is 1, save resources
; by storing 1! = 1 at ByteLo and
; skipping to end of program
cmpa #FactorMin
; Input = 1 skip to IsOne
beq IsOne
; Input > 1 continue with
; program
jmp NotOne
IsOne ldaa #1
staa ByteLo
jmp SkipToEnd
; Initialise initial ByteLo and
; subsequent factorial number
NotOne staa ByteLo
deca
staa FactorX
clra
clrb
; Load current factorial
; and initialise counts for
; Low and High bytes
Loop ldaa FactorX
staa CountLo
staa CountHi
; Load previous ByteHi value
; in to TempByteHi. Used for
; calculating subsequent ByteHi
ldaa ByteHi
staa TempByteHi
; Reset HiOrLo
ldaa #0
staa HiOrLo
; Test if previous result is
; an 8 or 16 bit number
tst ByteHi
; Previous result = 8 bit number
beq IfNoHiCalc
; Previous result = 16 bit number
jmp LoopLo
IfNoHiCalc inc HiOrLo
; Peform multiplication on low
; byte by adding ByteLo to itself
; CountLo times. IE, current
; factorial
LoopLo addb ByteLo
; If there is no carry, do not
; calculate HiByte and branch
; to NoCarry
bcc NoCarry
inca
NoCarry dec CountLo
; While LoopLo > 0, branch back
; to LoopLo and repeat
tst CountLo
bne LoopLo
; Store values of result at
; final result locations in
; memory
stab ByteLo
staa ByteHi
clra
clrb
; If previous number was 8 bit,
; don't calculate ByteHi and
; go branch back to Loop
tst HiOrLo
bne SkipLoopHi
; Peform multiplication on high
; byte by adding ByteHi to itself
; CountHi times. IE, current
; factorial
LoopHi addb TempByteHi
dec CountHi
; While LoopHi > 0, branch back
; to LoopHi and repeat
tst CountHi
bne LoopHi
; Load current ByteHi value
; together with previous
; ByteHi value to calculate
; new ByteHi value
ldaa ByteHi
aba
staa ByteHi
clra
clrb
; While FactorX > 0, branch back
; Loop and repeat
SkipLoopHi dec FactorX
tst FactorX
bne Loop
; Shortcut for terminating the
; program
.org $80
SkipToEnd jmp Terminate
; Used to determine if previous
; result was an 8 or 16 bit
; number
.org $90
HiOrLo .byte 0
; Used in a loop to perform
; multiplication on current
; factorial for low byte
.org $A0
CountLo .byte 0
; Used in a loop to perform
; multiplication on current
; factorial for high byte
.org $B0
CountHi .byte 0
; Current factorial
.org $C0
FactorX .byte 0
; Temporary location to store
; high byte from previous result.
; Used to calculate new high byte
; value
.org $D0
TempByteHi .byte 0
; Final result where high byte
; is stored
.org $0120
ByteHi .byte 0
; Final result where low byte is
; stored
.org $0121
ByteLo .byte 0
Terminate .end
; ### END OF PROGRAM ###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment