Initial commit of files related to NED architecture.
[ned1] / software / assembly_fragments / subroutines / sr_itoa.asm
itoa
# Description:
# Converts an integer in the decimal range 0..9 into the corresponding ASCII character value.
# Stack Requirements:
# Operand
# Return PC <-- TOS
# Copy operand to TOS
LDSP+1
# Verify that operand < 10
IM_10
LDSP+1
# Call subtract subroutine
# TODO: Put Return PC on TOS
# This requires a guarantee that I copy the PC in the 'correct' word, not too early. Will need NOP padding in nedasm.
# Make sure PC points to NEXT word, not to CURRENT word. Increment immediately after fetching a new word.
# Maybe define a JSR and RTS mnemonic pair that expands to the correct, real assembly in nedasm?
# Probably need to add a field to instruction_struct specifying that it should be the start of a new word. This also involves the code generation function.
# Assume low memory is:
# 0x00 Zero register
# 0x04 0x8000000 register
# 0x08 PC
# 0x0C PSW
# 0x10 reserved
# 0x14 reserved
# 0x18 reserved
# 0x1C reserved
# Implement this as:
# WORD_&subtract
# IM_0x08
# LOAD
# SWAP
# JMP
JSR>subtract
TEST # Set PSW according to difference.
# Branch if non-negative:
IM_12 # Address of PSW
LOAD
IM_2
AND
TEST
BRZ>itoahalt
# Verify that operand > -1
LDSP+0
TEST # Set PSW according to operand.
# Branch if negative:
IM_12
LOAD
IM_2
AND
IM_2
XOR
TEST
BRZ>itoahalt # Branch if operand was negative.
# Convert the integer to its ASCII representation and return to caller.
WORD_48
ADD
STSP+1
RTS # Implement as unconditional jump since Return PC is already on TOS.
# Halt on error
itoahalt
HALT