Code in Assembly Language, Raspberry Pi, GCC Compiler, Code Editor
In this example; the solution to a lab assignment has been provided. The equipments required to carry out this assignment are raspberry pi, gcc compiler, and text/code editor. This assignment has to be done on bright space. Here a custom shape generator has to be created. The code written in assembly language would generate boxes bordered by asterisks and filled with empty spaces. In this program a constant BOXSIZE is defined initially. The output of program exhibits a relationship between box size and four sides of a box.
SOLUTION: –
.equ BOXSIZE, 10
.data
asterisk: .ascii “*”
space: .ascii ” ”
newline: .ascii “\n”
.text
.global _start
_start:
PUSH {r4-r7, lr} @ save the registers and LR
@ print the box
MOV r4, #0 @ r4 will be the row index
forRow:
CMP r4, #BOXSIZE @ compare with box size
BGE endForRow @ if above size, end loop
MOV r5, #0 @ r5 will be the row index
forCol:
CMP r5, #BOXSIZE @ compare with box size
BGE endForCol @ if above size, end loop
CMP r4, #0 @ see if we are in the first row
BEQ border @ if first row, print border
CMP r4, #BOXSIZE – 1 @ see if we are in the last row
BEQ border @ if last row, print border
CMP r5, #0 @ see if we are at the first column
BEQ border @ if first column, print border
CMP r5, #BOXSIZE – 1 @ see if we are at the last column
BEQ border @ if last column, print border
interior:
BL printSpace @ if we get here we are at the box interior, print space
BAL next @ continue to next column
border:
BL printAsterisk @ we are at the box border, print asterisk
next:
ADD r5, r5, #1 @ increment the column
BAL forCol @ repeat cycle
endForCol:
BL printNewline @ print a newline
ADD r4, r4, #1 @ increment the row
BAL forRow @ repeat cycle
endForRow:
POP {r4-r7, lr} @ restore registers and LR
MOV r7, #1 @ set EXIT syscall
SWI 0 @ execute syscall
@ Print the string pointed by r0 of length r1 on the console
printString:
PUSH {r0-r2, r7, lr} @ save the registers and LR
MOV r2, r1 @ put string length in r2
MOV r1, r0 @ put string address in r0
MOV r0, #1 @ set the output to the console
MOV r7, #4 @ set the syscall to WRITE
SWI 0 @ make the syscall
POP {r0-r2, r7, lr} @ restore registers and LR
MOV pc, lr @ return to caller
@ Prints an asterisk
printAsterisk:
PUSH {r0, r7, lr} @ save the registers and LR
LDR r0, =asterisk @ use the asterisk string
MOV r1, #1 @ print one character
BL printString @ print using subroutine
POP {r0, r7, pc} @ restore registers and return to caller
@ Prints a space
printSpace:
PUSH {r0, r7, lr} @ save the registers and LR
LDR r0, =space @ use the space string
MOV r1, #1 @ print one character
BL printString @ print using subroutine
POP {r0, r7, pc} @ restore registers and return to caller
@ Prints a newline
printNewline:
PUSH {r0, r7, lr} @ save the registers and LR
LDR r0, =newline @ use the newline string
MOV r1, #1 @ print one character
BL printString @ print using subroutine
POP {r0, r7, pc} @ restore registers and return to caller
.end