Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- -------------------------------------------------------
- swap_array.s
- Working with stack frames and local variables.
- -------------------------------------------------------
- Author: david djukic
- ID: 160692920
- Email: djuk2920@mylaurier.ca
- Date: 23-03-17
- -------------------------------------------------------
- */
- .equ SWI_Exit, 0x11 @ Terminate program code
- .equ SWI_Open, 0x66 @ Open a file
- @ inputs - R0: address of file name, R1: mode (0: input, 1: write, 2: append)
- @ outputs - R0: file handle, -1 if open fails
- .equ SWI_Close, 0x68 @ Close a file
- @ inputs - R0: file handle
- .equ SWI_RdInt, 0x6c @ Read integer from a file
- @ inputs - R0: file handle
- @ outputs - R0: integer
- .equ SWI_PrInt, 0x6b @ Write integer to a file
- @ inputs - R0: file handle, R1: integer
- .equ SWI_RdStr, 0x6a @ Read string from a file
- @ inputs - R0: file handle, R1: buffer address, R2: buffer size
- @ outputs - R0: number of bytes stored
- .equ SWI_PrStr, 0x69 @ Write string to a file
- @ inputs- R0: file handle, R1: address of string
- .equ SWI_PrChr, 0x00 @ Write a character to stdout
- @ inputs - R0: character
- .equ inputMode, 0 @ Set file mode to input
- .equ outputMode, 1 @ Set file mode to output
- .equ appendMode, 2 @ Set file mode to append
- .equ stdout, 1 @ Set output target to be Stdout
- @-------------------------------------------------------
- @ Main Program
- MOV R0, #stdout
- @ Print the array before the swap
- LDR R2, =_Data
- LDR R1, =Data
- SUB R2, R2, R1
- STMFD SP!, {R2} @ Push length of list
- STMFD SP!, {R1} @ Push address of list
- BL print_array
- ADD SP, SP, #8
- BL PrintLF
- @ Swap the array data
- MOV R1, #1
- STMFD SP!, {R1} @ Push j
- MOV R1, #7
- STMFD SP!, {R1} @ Push i
- LDR R1, =Data
- STMFD SP!, {R1} @ Push a
- BL swap_array
- ADD SP, SP, #12
- BL PrintLF
- @ Print the array after the swap
- LDR R2, =_Data
- LDR R1, =Data
- SUB R2, R2, R1
- STMFD SP!, {R2} @ Push length of list
- STMFD SP!, {R1} @ Push address of list
- BL print_array
- ADD SP, SP, #8
- BL PrintLF
- SWI SWI_Exit
- @-------------------------------------------------------
- PrintLF:
- /*
- -------------------------------------------------------
- Prints the line feed character (\n)
- -------------------------------------------------------
- Uses:
- R0 - set to '\n'
- (SWI_PrChr automatically prints to stdout)
- -------------------------------------------------------
- */
- STMFD SP!, {R0, LR}
- MOV R0, #'\n' @ Define the line feed character
- SWI SWI_PrChr @ Print the character to Stdout
- LDMFD SP!, {R0, PC}
- @-------------------------------------------------------
- swap_array:
- /*
- -------------------------------------------------------
- Swaps location of two values in list. swap(a, i, j)
- Parameters passed on stack:
- Address of list
- Index of first value (i)
- Index of second value (j)
- Local variable
- temp
- -------------------------------------------------------
- Uses: R0- Address of array
- R1- First Index
- R2- Second Index
- R3- Swap register (value being swapped)
- -------------------------------------------------------
- */
- STMFD SP!, {FP, LR} @ push frame pointer and link register onto the stack
- MOV FP, SP @ save current stack top to frame pointer
- SUB SP, SP, #4 @ set aside space for temp
- STMFD SP!, {R0-R3} @ preserve other registers
- LDR R0, [FP, #8] @ load address of fp into R0
- LDR R1, [FP, #12] @ loads first Index
- LDR R2, [FP, #16] @loads second Index
- LDRB R3, [R0,R1] @loads first index & bytes from the address of r0 into R3
- STRB R3, [FP,#-4] @ stores bytes of R3 into -1 address of frame pointer
- LDRB R3, [R0,R2] @loads second index & bytes from r0 into R3
- STRB R3, [R0,R1] @stores bytes of R3 into address r0 + first index
- LDRB R3, [FP,#-4] @loads the bytes that was stored in -1 memory
- STRB R3, [R0,R2] @ store #bytes into the second index+address of r0
- LDMFD SP!, {R0,R3} @ pop preserved registers
- ADD SP, SP, #4 @ remove local storage
- LDMFD SP!, {FP, PC} @ pop frame pointer and program counter
- @-------------------------------------------------------
- print_array:
- /*
- -------------------------------------------------------
- Prints all integer values comma-separated in an array of bytes.
- (i.e. 2,4,7,1,6,...) with no trailing comma.
- Parameters passed on stack:
- Address of list
- Length of list
- -------------------------------------------------------
- Uses: R0- values for output
- R1- value being printed
- R2- First address (current)
- R3 - Length
- -------------------------------------------------------
- */
- STMFD SP!, {FP, LR} @load frame pointer and link register to stack
- MOV FP, SP @save stack top as FP
- STMFD SP!, {R0-R3} @preserve other registers
- MOV R0, #stdout @set r0 to stdout
- LDR R2,[FP,#8] @load starting address to R2
- LDR R3,[FP,#12] @load length of the list
- CMP R3,#0 @is the length = 0?
- BEG _Loop @if so go to _Loop (end)
- Loop:
- LDRB R1, [R2],#1 @load memory in current address
- SWI SWI_PrInt @print
- SUB R3,R3,#1 @sub 1 from length
- CMP R3, #0 @is the length=0?
- BEQ _Loop @if so go to end
- MOV R0, #',' @else add coma,
- SWI SWI_PrChr
- MOV R0, #' ' @ add space
- SWI SWI_PrChr
- MOV R0, #stdout @output value
- B Loop @looooooop
- _Loop:
- LDMFD SP!, {R0,R3} @pop registers
- LDMFD SP!, {FP,PC} @pop frame pointer and program counter
- @-------------------------------------------------------
- .data
- Data: .byte 4,5,9,0,8,0,8,7,1 @ The list of data
- _Data: @ End of list address
- .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement