Skip to content

Instantly share code, notes, and snippets.

@georgevanburgh
Created November 27, 2015 16:29
Show Gist options
  • Save georgevanburgh/3bfe68260746766a4902 to your computer and use it in GitHub Desktop.
Save georgevanburgh/3bfe68260746766a4902 to your computer and use it in GitHub Desktop.
Stack demonstration code from PASS1 workshop
; Move the stack pointer to point at the stack
ADRL SP, stack
; Define the strings we're going to print
s_div DEFB "Dividing ", 0
ALIGN
s_by DEFB " by ", 0
ALIGN
s_equals DEFB " equals ", 0
ALIGN
s_rem DEFB " remainder ", 0
ALIGN
start
; Print out 'dividing'
ADR R0, s_div;
SVC 3;
; Print 512
MOV R0, #512
SVC 4;
; Print out 'by 10'
ADR R0, s_by;
SVC 3;
MOV R0, #10
SVC 4;
; Print out 'equals'
ADR R0, s_equals;
SVC 3;
; Now do the division
MOV R0, #512;
BL div10;
SVC 4;
; Print out 'remainder'
ADR R0, s_rem;
SVC 3;
MOV R0, R1
SVC 4;
; Stop
SVC 2;
; N.b. div10 is an example method that divides an integer by ten.
; How it works is beyond the scope of COMP15111.
; However, you can see that the function pushes R2 on the stack, since
; otherwise it would be corrupted. R1 and R0 do not need to be pushed
; since div10 returns its result in those registers.
; takes argument in R0
; returns quotient in R0, remainder in R1
div10
; We're going to use these two registers in the function
PUSH {R2}; Or we could do 'STMFD SP!, {R2}'
; Magic division thing!
SUB R1, R0, #10 ; keep (x-10) for later
SUB R0, R0, R0, LSR #2
ADD R0, R0, R0, LSR #4
ADD R0, R0, R0, LSR #8
ADD R0, R0, R0, LSR #16
MOV R0, R0, LSR #3
ADD R2, R0, R0, ASL #2
SUBS R1, R1, R2, ASL #1 ; calc (x-10) - (x/10)*10
ADDPL R0, R0, #1 ; fix-up quotient
ADDMI R1, R1, #10 ; fix-up remainder
; We now get back the value of the registers we popped
POP {R2}; Or we could do 'LDMFD SP!, {R2}'
; Branch back to where we came from
MOV PC, LR
; Setup a stack area (we can point the SP here).
DEFS 1024
stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment