~yerinalexey/scalc-os

14970b9f50739066a305ff6bbc1f9a60ab995e46 — Alexey Yerin 4 months ago 93ae247 master
Implement memory load/store operations
2 files changed, 72 insertions(+), 2 deletions(-)

M README.md
M src/main.s
M README.md => README.md +2 -0
@@ 12,6 12,8 @@ implements only the surface functionality though - decimal integers and
* `d` - drop last value from the stack
* `s` - swap last two values
* `c` - push a copy of the last value
* `m<character>` - save last value to a named variable
* `l<character>` - push the value of a named variable

Interactions are done through the default input device, in case of most systems,
that would be serial. Entering a number to push it onto the stack works as

M src/main.s => src/main.s +70 -2
@@ 33,6 33,10 @@ kmain:
	li t0, 9
	beq a0, t0, exec_clone
	li t0, 10
	beq a0, t0, exec_mstore
	li t0, 11
	beq a0, t0, exec_mload
	li t0, 12
	beq a0, t0, exec_invalid
exec_number:
	add sp, sp, -8


@@ 126,6 130,49 @@ exec_clone:
	li a0, 'c'
	call printcln
	j 2f
exec_mstore:
	mv t4, a1 /* save a1 */
	ld t0, (sp)

	la t1, memory
	/* t1 = memory + a1 * 8 */
	li t2, 8
	mul t2, a1, t2
	add t1, t1, t2

	sd t0, (t1)

	li a0, 'm'
	call printc
	li a0, '('
	call printc
	mv a0, t4
	call printc
	li a0, ')'
	call printcln
	j 2f
exec_mload:
	mv t4, a1 /* save a1 */
	la t1, memory
	/* t1 = memory + a1 * 8 */
	li t2, 8
	mul t2, a1, t2
	add t1, t1, t2

	ld t0, (t1)

	add sp, sp, -8
	sd t0, (sp)

	li a0, 'l'
	call printc
	li a0, '('
	call printc
	mv a0, t4
	call printc
	li a0, ')'
	call printcln
	j 2f
exec_invalid:
	li a0, '?'
	call printcln


@@ 149,7 196,9 @@ exec_invalid:
        | 7 (drop)
        | 8 (swap)
        | 9 (clone)
        | 10 (invalid)
        | 10 (memory store)
        | 11 (memory load)
        | 12 (invalid)
*/
readatom:
	sd fp, -16(sp)


@@ 187,6 236,10 @@ retry:
	beq a0, t0, swap
	li t0, 'c'
	beq a0, t0, clone
	li t0, 'm'
	beq a0, t0, mstore
	li t0, 'l'
	beq a0, t0, mload

	/* 0 < (a0 - '0') < 9 */
	add a0, a0, -'0'


@@ 246,10 299,25 @@ swap:
clone:
	li a0, 9
	j 2f
invalid:
mstore:
	call readkey
	mv a1, a0
	li a0, 10
	j 2f
mload:
	call readkey
	mv a1, a0
	li a0, 11
	j 2f
invalid:
	li a0, 12
2:
	add sp, fp, 16
	ld ra, 8(fp)
	ld fp, (fp)
	ret

memory:
	.rept 256
	.quad 0
	.endr