Skip to content

Instantly share code, notes, and snippets.

@ryandhubbard
Created August 17, 2016 16:31
Show Gist options
  • Save ryandhubbard/29738f54b6f15371077f142481d3ab92 to your computer and use it in GitHub Desktop.
Save ryandhubbard/29738f54b6f15371077f142481d3ab92 to your computer and use it in GitHub Desktop.
assembly Rotate-13 encrypter
@@@ 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:
@ print an initial message to the screen
mov R0,#Stdout @print an initial message
ldr R1, =Message1 @ load address of Message1 label
swi SWI_PrStr @ display message to Stdout
@ == 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,=InputFileHandle @ if OK, load input file handle
str r0,[r1] @ save the file handle
@ == Read Strings until end of file =============================
RLoop:
ldr r0,=InputFileHandle
ldr r0,[r0]
ldr r1,=CharArray
mov r2,#80
swi 0x6a
bcs EofReached
mov r2, r0 @count of bytes read
ldr r3, =CharArray
parseLoop:
ldrb r0,[r3]
cmp r0, #10
beq skip_char
cmp r0, #14
beq skip_char
cmp r0, #64
ble printCharacter
cmp r0, #77
ble add13
cmp r0, #90
ble sub13
cmp r0, #96
ble printCharacter
cmp r0, #109
ble add13
cmp r0, #122
ble sub13
bgt printCharacter
add13:
adds r0, r0, #13
bal printCharacter
sub13:
subs r0, r0, #13
bal printCharacter
printCharacter:
mov r1, r0
@mov R0,#Stdout
swi SWI_PrChr
adds r3, r3, #1
subs r2, r2, #1
cmp r2, #0x00
bne parseLoop
skip_char:
mov R0,#Stdout @ print new line
ldr R1, =NL
swi SWI_PrStr
bal RLoop
@ == End of file ===============================================
EofReached:
mov R0, #Stdout @ print last message
ldr R1, =EndOfFileMsg
swi SWI_PrStr
@ == Close a file ===============================================
ldr R0, =InputFileHandle @ get address of file handle
ldr R0, [R0] @ get value at address
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
CharArray: .skip 80
InputFileHandle: .skip 4
InFileName: .asciz "lab2.txt"
FileOpenInpErrMsg: .asciz "\nFailed to open input file \n"
EndOfFileMsg: .asciz "\nEnd of file reached\n"
Message1: .asciz "\nWelcome to ROT13 Encoder \n"
NL: .asciz "\n" @ new line
.end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment