fixed a bug with the multiply function in core.dsa and added a print_num
function to print.dsa for decimal numbers
This commit is contained in:
@@ -28,6 +28,11 @@
|
|||||||
// push pcx
|
// push pcx
|
||||||
// jmp print::print_word
|
// jmp print::print_word
|
||||||
//
|
//
|
||||||
|
// usage for print_num:
|
||||||
|
// push (register containing number to print in decimal)
|
||||||
|
// push pcx
|
||||||
|
// jmp print::print_num
|
||||||
|
//
|
||||||
|
|
||||||
include maths "../maths/core.dsa"
|
include maths "../maths/core.dsa"
|
||||||
|
|
||||||
@@ -209,6 +214,65 @@ print_newline:
|
|||||||
// _end saves the display state
|
// _end saves the display state
|
||||||
jmp _end
|
jmp _end
|
||||||
|
|
||||||
|
// ------------------------------------------
|
||||||
|
// prints arg[0] as a decimal number to the screen.
|
||||||
|
print_num:
|
||||||
|
push bpr
|
||||||
|
mov spr, bpr
|
||||||
|
|
||||||
|
ldw bpr, rg0, 8 // load number to print
|
||||||
|
lli 0, rg5 // rg5 = digit counter
|
||||||
|
|
||||||
|
// check if number is zero
|
||||||
|
cmp rg0, zero
|
||||||
|
jne _print_num_extract_digits
|
||||||
|
|
||||||
|
// special case: print '0' for zero
|
||||||
|
lli 0x30, rg6
|
||||||
|
push rg6 // push digit to stack buffer
|
||||||
|
lli 1, rg5 // we have 1 digit
|
||||||
|
jmp _print_num_output
|
||||||
|
|
||||||
|
_print_num_extract_digits:
|
||||||
|
// divide by 10 repeatedly to get digits
|
||||||
|
cmp rg0, zero
|
||||||
|
jeq _print_num_output
|
||||||
|
|
||||||
|
// call divmod(rg0, 10)
|
||||||
|
push rg0 // dividend
|
||||||
|
lli 10, rg1
|
||||||
|
push rg1 // divisor (10)
|
||||||
|
call maths::divmod
|
||||||
|
pop rg0 // quotient (continue dividing this)
|
||||||
|
pop rg1 // remainder (the digit)
|
||||||
|
|
||||||
|
// convert digit to ASCII and push to stack buffer
|
||||||
|
addi rg1, 0x30, rg6 // convert to ASCII
|
||||||
|
push rg6 // push digit to stack
|
||||||
|
inc rg5 // increment digit counter
|
||||||
|
|
||||||
|
jmp _print_num_extract_digits
|
||||||
|
|
||||||
|
_print_num_output:
|
||||||
|
// now print digits (pop them off in reverse order)
|
||||||
|
ldw current, rg1 // get display pointer
|
||||||
|
|
||||||
|
_print_num_output_loop:
|
||||||
|
// check if we've printed all digits
|
||||||
|
cmp rg5, zero
|
||||||
|
jeq _print_num_done
|
||||||
|
|
||||||
|
// pop digit and print it
|
||||||
|
pop rg6
|
||||||
|
stb rg6, rg1
|
||||||
|
addi rg1, 1
|
||||||
|
dec rg5
|
||||||
|
|
||||||
|
jmp _print_num_output_loop
|
||||||
|
|
||||||
|
_print_num_done:
|
||||||
|
jmp _end
|
||||||
|
|
||||||
// ------------------------------------------
|
// ------------------------------------------
|
||||||
// resets the cursor position on the screen to 0x20000. (0,0)
|
// resets the cursor position on the screen to 0x20000. (0,0)
|
||||||
reset:
|
reset:
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ multiply:
|
|||||||
|
|
||||||
ldw bpr, rg0, 8 // load op 2
|
ldw bpr, rg0, 8 // load op 2
|
||||||
ldw bpr, rg1, 12 // load op 1
|
ldw bpr, rg1, 12 // load op 1
|
||||||
|
lwi 0, rg2 // initialise rg2 to zero
|
||||||
|
|
||||||
_multiply_loop:
|
_multiply_loop:
|
||||||
add rg2, rg0, rg2
|
add rg2, rg0, rg2
|
||||||
|
|||||||
Reference in New Issue
Block a user