Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;calculates the result of squaring the number. example :
- ;10 squared = sum of adding 10, ten times.
- BR main
- num: .word 0
- val: .word 0
- res: .word 0
- msg1: .ascii "Number: \x00"
- msg2: .ascii " is invalid \x00"
- msg3: .ascii " Squared = \x00"
- checkVal: LDA 2,s
- CPA 0,i ;checks if number > 0
- brgt valid
- LDA 0,i ; puts 0 in the return value(to be interpreted as 'false')
- STA 4,s
- RET0
- valid: LDA 1,i ; puts 1 in the return value(to be interpreted as 'true')
- STA 4,s
- RET0
- sqNum: SUBSP 4,i ; 2 local variables
- LDA 0,i
- STA 2,s
- while: LDA 2,s ;squares the number
- CPA 6,s
- BRGE out
- LDA 2,s
- ADDA 1,i
- STA 2,s
- LDA 0,s
- ADDA 6,s
- STA 0,s
- BR while
- out: LDA 0,s ; done calculating the result
- STA 8,s ; save result in return value.
- ADDSP 4,i
- RET0
- main: DECI num,d
- SUBSP 2,i ; return value
- SUBSP 2,i ; parameter
- LDA num,d
- STA 0,s
- CALL checkVal
- ADDSP 2,i ; parameters go away
- LDA 0,s
- STA val,d
- ADDSP 2,i ;clear return value
- LDA val,d
- brgt cont
- STRO msg1,d
- DECO num,d
- STRO msg2,d
- BR done
- cont: SUBSP 2,i ; return value
- SUBSP 2,i ;parameter
- LDA num,d
- STA 0,s
- CALL sqNum
- ADDSP 2,i ; clear parameter
- LDA 0,s
- STA res,d
- ADDSP 2,i ; clear return value
- STRO msg1,d
- DECO num,d
- STRO msg3,d
- DECO res,d
- done: STOP
- .end
Add Comment
Please, Sign In to add comment