| 1 | itoa |
| 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 |
| 10 | LDSP+1 |
| 11 | |
| 12 | |
| 13 | # Verify that operand < 10 |
| 14 | IM_10 |
| 15 | LDSP+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 |
| 37 | JSR>subtract |
| 38 | TEST # Set PSW according to difference. |
| 39 | # Branch if non-negative: |
| 40 | IM_12 # Address of PSW |
| 41 | LOAD |
| 42 | IM_2 |
| 43 | AND |
| 44 | TEST |
| 45 | BRZ>itoahalt |
| 46 | |
| 47 | |
| 48 | # Verify that operand > -1 |
| 49 | LDSP+0 |
| 50 | TEST # Set PSW according to operand. |
| 51 | # Branch if negative: |
| 52 | IM_12 |
| 53 | LOAD |
| 54 | IM_2 |
| 55 | AND |
| 56 | IM_2 |
| 57 | XOR |
| 58 | TEST |
| 59 | BRZ>itoahalt # Branch if operand was negative. |
| 60 | |
| 61 | # Convert the integer to its ASCII representation and return to caller. |
| 62 | WORD_48 |
| 63 | ADD |
| 64 | STSP+1 |
| 65 | RTS # Implement as unconditional jump since Return PC is already on TOS. |
| 66 | |
| 67 | |
| 68 | # Halt on error |
| 69 | itoahalt |
| 70 | HALT |
| 71 | |