Skip to main content

Collection of Functions

Operational

saveRegisters

Saves the context of the registers for later recovery and loads the value of the arguments in these. To be used at the beginning of other functions.

Arguments

None

Returns

Nothing

saveRegisters:
pop r0 ; Stores the return address of the function
push r1
push r2
push r3
push r4
push r5
push r6
push r7
push r0 ; Puts it back on top of the stack
load r1, Arg1
load r2, Arg2
load r3, Arg3
load r4, Arg4
load r5, Arg5
load r6, Arg6
load r7, Arg7
loadn r0, #0
rts ; Unstacks and returns.

restoreRegisters

Recovers the context of the registers from the stack. To be used at the end of functions.

Arguments

None

Returns

Nothing

restoreRegisters:
pop r0
pop r7
pop r6
pop r5
pop r4
pop r3
pop r2
pop r1
push r0
loadn r0, #0
rts
warning

The functions saveRegisters and restoreRegisters are required for the operation of many other functions displayed here.

Mathematics

screenPosition

Given values for line and column, returns the index that corresponds to the expected position on the screen. Values greater than those defined in HEIGHT and WIDTH describe turns on the screen.

Arguments

LabelExpected Value
Arg1Line
Arg2Column
HEIGHTScreen height
WIDTHScreen width

Returns

LabelExpected Value
RetIndex of the position on the screen
screenPosition:
call saveRegisters
load r3, HEIGHT
load r4, WIDTH
mod r1, r1, r3
mul r1, r1, r4
mod r2, r2, r4
add r1, r1, r2
store Ret, r1
call restoreRegisters
rts

Printing

printIntR

Prints an integer on the screen, aligned to the right.

Arguments

LabelExpected Value
Arg1Integer to be printed
Arg2Position on the screen where printing starts
Arg3Color value to print the number

Returns

Nothing

printIntR:
call saveRegisters
loadn r4, #10 ; loads the value 10 for applying the modulo operation
loadn r5, #'0' ; loads the index value of the character 0

pirLoop: ; starts the printing loop
mod r6, r1, r4 ; gets the least significant digit of r1
add r6, r5, r6 ; applies it as a shift in the character map
add r6, r6, r3 ; applies the color value
outchar r6, r2 ; prints the character at position r2
div r1, r1, r4 ; integer division of r1 by 10
jz pirEnd ; exits the loop if r1 = 0
dec r2 ; otherwise, decrements r2
jmp pirLoop ; and continues

pirEnd:
call restoreRegisters
rts

Input

takeInput

Function that delays the execution of the game for a period to obtain an input from the player. If the input is the expected one, the delay is canceled.

Arguments

LabelExpected Value
Arg1Delay factor
Arg2Expected input

Returns

LabelExpected Value
Ret0, if no key was pressed, otherwise, the ASCII value of the pressed key
takeInput:
call saveRegisters
loadn r3, #1000 ; Base delay, implies a microsecond in 1 MHz
loadn r4, #255 ; Standard value returned by inchar when no key press is detected
store Ret, r0

delay_A:
mov r5, r1
delay_B:
inchar r6
cmp r4, r6
jeq delayContinue
store Ret, r6
cmp r2, r6
jeq delayEnd
delayContinue:
dec r5
jnz delay_B
dec r3
jnz delay_A
delayEnd:
call restoreRegisters
rts
tip

The takeInput function can be used to give the game a given interval between actions. To do this, simply pass the expected value to interrupt the interval as one that cannot be matched: 0.