Skip to content

Instantly share code, notes, and snippets.

@ryandhubbard
Created August 17, 2016 16:33
Show Gist options
  • Save ryandhubbard/7be953355137c2f6999be4b2f371e5f2 to your computer and use it in GitHub Desktop.
Save ryandhubbard/7be953355137c2f6999be4b2f371e5f2 to your computer and use it in GitHub Desktop.
ARM7 assembly program Sort integers
@@@ OPEN INPUT FILE, READ INTEGER FROM FILE, PRINT IT, CLOSE INPUT FILE
.equ SWI_Open, 0x66 @open a file
.equ SWI_Close,0x68 @close a file
.equ SWI_PrChr,0x00 @ Write an ASCII char to Stdout
.equ SWI_PrStr, 0x69 @ Write a null-ending string
.equ SWI_PrInt,0x6b @ Write an Integer
.equ SWI_RdInt,0x6c @ Read an Integer from a file
.equ Stdout, 1 @ Set output target to be Stdout
.equ SWI_Exit, 0x11 @ Stop execution
.global _start
.text
_start:
@ == Open an input file for reading =============================
@ if problems, print message to Stdout and exit
ldr r0,=InFileName @ set Name for input file
mov r1,#0 @ mode is input
swi SWI_Open @ open file for input
bcs InFileError @ Check Carry-Bit (C): if= 1 then ERROR
@ Save the file handle in memory:
ldr r1,=InFileHandle @ if OK, load input file handle
str r0,[r1] @ save the file handle
@ == Read integers until end of file ============================= RLoop:
RLoop:
ldr r0,=InFileHandle @ load input file handle
ldr r0,[r0]
swi SWI_RdInt @ read the integer into R0
bcs EofReached @ Check Carry-Bit (C): if= 1 then EOF reached
@ print the integer to Stdout
mov r1,r0 @ R1 = integer to print
mov R0,#Stdout
swi SWI_PrInt
mov R0,#Stdout @ print new line
ldr r1, =NL
swi SWI_PrStr
bal RLoop
@ keep reading till end of file
@ == End of file ===============================================
EofReached:
mov R0, #Stdout @ print last message ldr R1, =EndOfFileMsg
swi SWI_PrStr
@ == Close a file ===============================================
ldr R0, =InFileHandle
ldr R0, [R0]
swi SWI_Close
@================================================================
Exit:
swi SWI_Exit @ stop executing
InFileError:
mov R0, #Stdout
ldr R1, =FileOpenInpErrMsg
swi SWI_PrStr
bal Exit @ give up, go to end
@================================================================
.data
.align
Array: .skip 20 * 4
InFileHandle: .skip 4
InFileName: .asciz "input.txt"
FileOpenInpErrMsg: .asciz "Failed to open input file \n"
EndOfFileMsg: .asciz "End of file reached\n"
ColonSpace: .asciz": "
NL: .asciz "\n " @ new line
.end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment