forked from TCShenanigans/symphony_stdlib
Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
993bbb797d | ||
|
|
7a9f2da1c0 | ||
|
|
db8462a2fe | ||
|
|
aa5cc1dbe0 | ||
|
|
f7d3ca2ec5 | ||
|
|
da811fd65d | ||
|
|
49767b61df | ||
|
|
6eb91f3433 | ||
|
|
9220e3dc28 | ||
|
|
25ecd155e1 | ||
|
|
c071a00746 | ||
|
|
5400da0157 | ||
|
|
f19dfd3611 | ||
|
|
a9fa16a241 | ||
|
|
de9ac7fe72 | ||
|
|
1fc4515f6b | ||
|
|
f4ae43bbc1 | ||
|
|
f27715b5b3 |
+2
-2
@@ -16,8 +16,8 @@ All functions in the standard library should follow the following outline:
|
|||||||
; Arguments: <which register contains what argument>
|
; Arguments: <which register contains what argument>
|
||||||
; Result: <what is the result, and where is it stored>
|
; Result: <what is the result, and where is it stored>
|
||||||
; Clobbers: <list of registers that are clobbered>
|
; Clobbers: <list of registers that are clobbered>
|
||||||
<label>: <;SHOULD BE INLINED>
|
fn_label: <;SHOULD BE INLINED>
|
||||||
<CODE>
|
CODE
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# Memory Map
|
||||||
|
|
||||||
|
To not have to allocate a ton of things at run time the Standard Library uses a static memory map for some purposes
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
| Start | Use |
|
||||||
|
| ---- | ---- |
|
||||||
|
| `0x0` | Reset Vector |
|
||||||
|
| `0x16` | Zero Page |
|
||||||
|
| `0x100` | User Code |
|
||||||
|
| `?` | Library Code |
|
||||||
|
| `0x1_0000` | LUTs |
|
||||||
|
| `?` | heap |
|
||||||
|
| `0xXXF0_0000` | Stack |
|
||||||
|
| `0xXXFF_0000` | quick access |
|
||||||
|
|
||||||
|
## Zero Page
|
||||||
|
Some values are needed not often enough to get their own special register, but often enough that it makes sense to keep them at a quickly accessible location.
|
||||||
|
This is what the zero page is for. its position in memory allows us to load them into a register with a single instruction.
|
||||||
|
|
||||||
@@ -4,6 +4,7 @@ This is a standard library for symphony.
|
|||||||
It is both intended as a practical toolkit to develop more complex software as well as a teaching resource.
|
It is both intended as a practical toolkit to develop more complex software as well as a teaching resource.
|
||||||
|
|
||||||
If you just want to use the standard library [[stdlib.asm]] is your main header, include it after your code.
|
If you just want to use the standard library [[stdlib.asm]] is your main header, include it after your code.
|
||||||
|
You also need to include [[globals.asm]] as the first line in your assembly file.
|
||||||
|
|
||||||
If you are using it as a learning resource have a look at the [teaching folder](teaching).
|
If you are using it as a learning resource have a look at the [teaching folder](teaching).
|
||||||
|
|
||||||
|
|||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
jmp 0x100
|
||||||
|
|
||||||
|
@0x10
|
||||||
|
screen:
|
||||||
|
frambuffer_ptr: U32 0x0
|
||||||
|
size: U32 0x0
|
||||||
|
position_xy: U32 0x0
|
||||||
|
mode: U32 0x0
|
||||||
|
|
||||||
|
@0x100
|
||||||
@@ -1,15 +1,8 @@
|
|||||||
; Multiplies r1 and r2, returning the lower part of the result
|
|
||||||
; Arguments:
|
|
||||||
; r1 - The first value
|
|
||||||
; r2 - The second value
|
|
||||||
; Result:
|
|
||||||
; r1 - The lower 32 bits of the result
|
|
||||||
; Clobbers: r2, r3, r4, r5
|
|
||||||
pub mul_low:
|
pub mul_low:
|
||||||
mov r3, 0 ; result
|
mov r3, 0 ; result
|
||||||
mov r4, 31 ; loop counter
|
mov r4, 31 ; loop counter
|
||||||
|
|
||||||
mul_low_loop:
|
mull_loop:
|
||||||
asr r5, r2, 31
|
asr r5, r2, 31
|
||||||
and r5, r5, r1
|
and r5, r5, r1
|
||||||
lsl r5, r5, r4
|
lsl r5, r5, r4
|
||||||
@@ -17,18 +10,14 @@ pub mul_low:
|
|||||||
lsl r2, r2, 1
|
lsl r2, r2, 1
|
||||||
sub r4, r4, 1
|
sub r4, r4, 1
|
||||||
cmp r4, 0
|
cmp r4, 0
|
||||||
jge mul_low_loop
|
jge mull_loop
|
||||||
mov r1, r3
|
mov r1, r3
|
||||||
|
|
||||||
jmp r13
|
jmp r13
|
||||||
|
|
||||||
; Calculates the absolute value of the value provided in the r1 register
|
; Calculates the absolute value of the value provided in the r1 register
|
||||||
; Arguments:
|
; Based on Stanford's BitHacks
|
||||||
; r1 - The value for which we want the absolute value
|
; Clobbers r2
|
||||||
; Result:
|
|
||||||
; r1 - The calculated absolute value
|
|
||||||
; Clobbers: r2
|
|
||||||
; Info: Based on Stanford's BitHacks
|
|
||||||
pub abs: ; SHOULD BE INLINED
|
pub abs: ; SHOULD BE INLINED
|
||||||
; mask = v >> 31
|
; mask = v >> 31
|
||||||
asr r2, r1, 31
|
asr r2, r1, 31
|
||||||
@@ -39,13 +28,8 @@ pub abs: ; SHOULD BE INLINED
|
|||||||
jmp r13
|
jmp r13
|
||||||
|
|
||||||
; Calculates the minimum value of the two values provided in the r1 and r2 registers
|
; Calculates the minimum value of the two values provided in the r1 and r2 registers
|
||||||
; Arguments:
|
; Based on Stanford's BitHacks
|
||||||
; r1 - The first value
|
; Clobbers flags
|
||||||
; r2 - The second value
|
|
||||||
; Result:
|
|
||||||
; r1 - The smaller value
|
|
||||||
; Clobbers: Nothing
|
|
||||||
; Info: Based on Stanford's BitHacks
|
|
||||||
pub min: ; SHOULD BE INLINED
|
pub min: ; SHOULD BE INLINED
|
||||||
; x < y
|
; x < y
|
||||||
cmp r1, r2
|
cmp r1, r2
|
||||||
@@ -61,13 +45,8 @@ pub min: ; SHOULD BE INLINED
|
|||||||
jmp r13
|
jmp r13
|
||||||
|
|
||||||
; Calculates the maximum value of the two values provided in the r1 and r2 registers
|
; Calculates the maximum value of the two values provided in the r1 and r2 registers
|
||||||
; Arguments:
|
; Based on Stanford's BitHacks
|
||||||
; r1 - The first value
|
; Clobbers r2 and flags
|
||||||
; r2 - The second value
|
|
||||||
; Result:
|
|
||||||
; r1 - The smaller value
|
|
||||||
; Clobbers: r2
|
|
||||||
; Info: Based on Stanford's BitHacks
|
|
||||||
pub max: ; SHOULD BE INLINED
|
pub max: ; SHOULD BE INLINED
|
||||||
; x < y
|
; x < y
|
||||||
cmp r1, r2
|
cmp r1, r2
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
pub include bit
|
pub include bit
|
||||||
pub include imath
|
pub include imath
|
||||||
pub include strint
|
|
||||||
-145
@@ -1,145 +0,0 @@
|
|||||||
; Convert string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
pub fn_atoi:
|
|
||||||
load_8 r3, [r1]
|
|
||||||
cmp r3, 0x30 ; '0'
|
|
||||||
jne fn_atoi_dec ; No prefix, parse decimal
|
|
||||||
add r1, r1, 1
|
|
||||||
load_8 r3, [r1] ; From "boxBOX", or an octal digit
|
|
||||||
cmp r3, 0x30 ; '0'
|
|
||||||
jl atoi_badprefix
|
|
||||||
cmp r3, 0x37 ; '7'
|
|
||||||
jle fn_atoi_oct
|
|
||||||
add r1, r1, 1 ; Not octal; 2-byte prefix
|
|
||||||
and r3, r3, 0x5f ; to uppercase
|
|
||||||
cmp r3, 0x42 ; 'B'
|
|
||||||
je fn_atoi_bin
|
|
||||||
cmp r3, 0x4F ; 'O'
|
|
||||||
je fn_atoi_oct
|
|
||||||
cmp r3, 0x58 ; 'X'
|
|
||||||
je fn_atoi_hex
|
|
||||||
|
|
||||||
; not a valid prefix, but we did see a zero
|
|
||||||
atoi_badprefix:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
; Only exists because conditional register jumps are not allowed
|
|
||||||
atoi_done:
|
|
||||||
jmp r13
|
|
||||||
|
|
||||||
; Convert decimal string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
pub fn_atoi_dec:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
atoi_dec_loop:
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x30 ; '0'
|
|
||||||
jl atoi_done
|
|
||||||
cmp r3, 0x39 ; '9'
|
|
||||||
jg atoi_done
|
|
||||||
add r2, r2, 1
|
|
||||||
sub r3, r3, 0x30 ; '0'
|
|
||||||
lsl flags, r1, 2
|
|
||||||
add r1, r1, flags
|
|
||||||
lsl r1, r1, 1
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_dec_loop
|
|
||||||
|
|
||||||
; Convert binary string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
pub fn_atoi_bin:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
atoi_bin_loop:
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x30 ; '0'
|
|
||||||
jl atoi_done
|
|
||||||
cmp r3, 0x39 ; '1'
|
|
||||||
jg atoi_done
|
|
||||||
add r2, r2, 1
|
|
||||||
sub r3, r3, 0x30 ; '0'
|
|
||||||
lsl r1, r1, 1
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_bin_loop
|
|
||||||
|
|
||||||
; Convert octal string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
pub fn_atoi_oct:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
atoi_oct_loop:
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x30 ; '0'
|
|
||||||
jl atoi_done
|
|
||||||
cmp r3, 0x39 ; '7'
|
|
||||||
jg atoi_done
|
|
||||||
add r2, r2, 1
|
|
||||||
sub r3, r3, 0x30 ; '0'
|
|
||||||
lsl r1, r1, 3
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_oct_loop
|
|
||||||
|
|
||||||
; Convert hexadecimal string to integer
|
|
||||||
; Arguments:
|
|
||||||
; r1 - Pointer to string
|
|
||||||
; Result:
|
|
||||||
; r1 - Parsed integer
|
|
||||||
; r2 - Pointer to first rejected input byte
|
|
||||||
; Clobbers:
|
|
||||||
; flags
|
|
||||||
; r3 - last character read
|
|
||||||
pub fn_atoi_hex:
|
|
||||||
mov r2, r1
|
|
||||||
mov r1, 0
|
|
||||||
atoi_hex_loop:
|
|
||||||
load_8 r3, [r2]
|
|
||||||
cmp r3, 0x30 ; '0'
|
|
||||||
jl atoi_done
|
|
||||||
cmp r3, 0x39 ; '9'
|
|
||||||
jg atoi_hex_letter
|
|
||||||
add r2, r2, 1
|
|
||||||
sub r3, r3, 0x30 ; '0'
|
|
||||||
lsl r1, r1, 4
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_hex_loop
|
|
||||||
|
|
||||||
atoi_hex_letter:
|
|
||||||
and r3, r3, 0x5f ; to uppercase
|
|
||||||
cmp r3, 0x41 ; 'A'
|
|
||||||
jl atoi_done
|
|
||||||
cmp r3, 0x46 ; 'F'
|
|
||||||
jg atoi_done
|
|
||||||
add r2, r2, 1
|
|
||||||
sub r3, r3, 0x37 ; 'A' - 10
|
|
||||||
lsl r1, r1, 4
|
|
||||||
add r1, r1, r3
|
|
||||||
jmp atoi_hex_loop
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
mov r1, 1
|
|
||||||
mov r2, test1
|
|
||||||
mov r3, test1_end
|
|
||||||
mov r13, test1_pass
|
|
||||||
jmp atoi_test
|
|
||||||
test1_pass:
|
|
||||||
|
|
||||||
mov r1, 2
|
|
||||||
mov r2, test2
|
|
||||||
mov r3, test2_end
|
|
||||||
mov r13, test2_pass
|
|
||||||
jmp atoi_test
|
|
||||||
test2_pass:
|
|
||||||
|
|
||||||
mov r1, 3
|
|
||||||
mov r2, test3
|
|
||||||
mov r3, test3_end
|
|
||||||
mov r13, test3_pass
|
|
||||||
jmp atoi_test
|
|
||||||
test3_pass:
|
|
||||||
|
|
||||||
mov r1, 4
|
|
||||||
mov r2, test4
|
|
||||||
mov r3, test4_end
|
|
||||||
mov r13, test4_pass
|
|
||||||
jmp atoi_test
|
|
||||||
test4_pass:
|
|
||||||
|
|
||||||
mov r1, 5
|
|
||||||
mov r2, test5
|
|
||||||
mov r3, test5_end
|
|
||||||
mov r13, test5_pass
|
|
||||||
jmp atoi_test
|
|
||||||
test5_pass:
|
|
||||||
|
|
||||||
success:
|
|
||||||
jmp success
|
|
||||||
|
|
||||||
; test strint.fn_atoi
|
|
||||||
; Arguments:
|
|
||||||
; r1 - test number
|
|
||||||
; r2 - test data label
|
|
||||||
; r3 - test end-of-string label
|
|
||||||
; Result:
|
|
||||||
; void. Hangs in failure: on failure.
|
|
||||||
; Clobbers:
|
|
||||||
; r8 - test number
|
|
||||||
; r9 - Expected result in r1
|
|
||||||
; r10 - Expected result in r2
|
|
||||||
; Notes:
|
|
||||||
; Depends on fn_atoi not clobbering r8-r10.
|
|
||||||
atoi_test:
|
|
||||||
mov r8, r1
|
|
||||||
load_32 r9, [r2]
|
|
||||||
add r1, r2, 4
|
|
||||||
mov r10, r3
|
|
||||||
push r13
|
|
||||||
mov r13, atoi_test_ret
|
|
||||||
jmp stdlib.strint.fn_atoi
|
|
||||||
atoi_test_ret:
|
|
||||||
pop r13
|
|
||||||
cmp r1, r9
|
|
||||||
jne failure
|
|
||||||
cmp r2, r10
|
|
||||||
jne failure
|
|
||||||
jmp r13
|
|
||||||
|
|
||||||
failure:
|
|
||||||
jmp failure
|
|
||||||
|
|
||||||
test1: U32 0 "0"
|
|
||||||
test1_end: "\0"
|
|
||||||
|
|
||||||
test2: U32 1 "1"
|
|
||||||
test2_end: "\0"
|
|
||||||
|
|
||||||
test3: U32 2 "2"
|
|
||||||
test3_end: "\0"
|
|
||||||
|
|
||||||
test4: U32 42 "42"
|
|
||||||
test4_end: "\0"
|
|
||||||
|
|
||||||
test5: U32 67 "67"
|
|
||||||
test5_end: "lol"
|
|
||||||
|
|
||||||
include stdlib
|
|
||||||
Reference in New Issue
Block a user