~yerinalexey/scalc-os

a6fc662be99665a5d53dc1f1f47a358bed202efb — Alexey Yerin 4 months ago a207514
Implement a swap command
2 files changed, 19 insertions(+), 3 deletions(-)

M README.md
M src/main.s
M README.md => README.md +1 -0
@@ 10,6 10,7 @@ implements only the surface functionality though - decimal integers and

* `.` - print last value in the stack
* `d` - drop last value from the stack
* `s` - swap two last values

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 +18 -3
@@ 29,6 29,8 @@ kmain:
	li t0, 7
	beq a0, t0, exec_drop
	li t0, 8
	beq a0, t0, exec_swap
	li t0, 9
	beq a0, t0, exec_invalid
exec_number:
	add sp, sp, -8


@@ 107,12 109,19 @@ exec_drop:
	li a0, 'd'
	call printcln
	j 2f
exec_swap:
	ld t0, (sp)
	ld t1, 8(sp)
	sd t1, (sp)
	sd t0, 8(sp)
	li a0, 's'
	call printcln
	j 2f
exec_invalid:
	li a0, '?'
	call printcln
2:
	j 1b

3:
	add sp, fp, 16
	ld ra, 8(fp)


@@ 129,7 138,8 @@ exec_invalid:
        | 5 (remainder)
        | 6 (print)
        | 7 (drop)
        | 8 (invalid)
        | 8 (swap)
        | 9 (invalid)
*/
readatom:
	sd fp, -16(sp)


@@ 163,6 173,8 @@ retry:
	beq a0, t0, printstack
	li t0, 'd'
	beq a0, t0, drop
	li t0, 's'
	beq a0, t0, swap

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


@@ 216,8 228,11 @@ printstack:
drop:
	li a0, 7
	j 2f
invalid:
swap:
	li a0, 8
	j 2f
invalid:
	li a0, 9
2:
	add sp, fp, 16
	ld ra, 8(fp)