Initial commit of files related to NED architecture.
[ned1] / software / assembly_fragments / subroutines / sr_itoa.asm
CommitLineData
bc5b63cf
AT
1itoa
2# Description:
3# Converts an integer in the decimal range 0..9 into the corresponding ASCII character value.
4# Stack Requirements:
5# Operand
6# Return PC <-- TOS
7
8
9# Copy operand to TOS
10LDSP+1
11
12
13# Verify that operand < 10
14IM_10
15LDSP+1
16# Call subtract subroutine
17# TODO: Put Return PC on TOS
18# This requires a guarantee that I copy the PC in the 'correct' word, not too early. Will need NOP padding in nedasm.
19# Make sure PC points to NEXT word, not to CURRENT word. Increment immediately after fetching a new word.
20# Maybe define a JSR and RTS mnemonic pair that expands to the correct, real assembly in nedasm?
21# 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.
22# Assume low memory is:
23# 0x00 Zero register
24# 0x04 0x8000000 register
25# 0x08 PC
26# 0x0C PSW
27# 0x10 reserved
28# 0x14 reserved
29# 0x18 reserved
30# 0x1C reserved
31# Implement this as:
32# WORD_&subtract
33# IM_0x08
34# LOAD
35# SWAP
36# JMP
37JSR>subtract
38TEST # Set PSW according to difference.
39# Branch if non-negative:
40IM_12 # Address of PSW
41LOAD
42IM_2
43AND
44TEST
45BRZ>itoahalt
46
47
48# Verify that operand > -1
49LDSP+0
50TEST # Set PSW according to operand.
51# Branch if negative:
52IM_12
53LOAD
54IM_2
55AND
56IM_2
57XOR
58TEST
59BRZ>itoahalt # Branch if operand was negative.
60
61# Convert the integer to its ASCII representation and return to caller.
62WORD_48
63ADD
64STSP+1
65RTS # Implement as unconditional jump since Return PC is already on TOS.
66
67
68# Halt on error
69itoahalt
70HALT
71