Print itself: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) No edit summary |
|||
Line 173: | Line 173: | ||
$ wren self_print.wren |
$ wren self_print.wren |
||
</pre> |
</pre> |
||
{{header|Z80 Assembly}} |
|||
This program prints its bytecode as text to the Amstrad CPC terminal. The program is run by typing <code>call &1000</code> to the screen. |
|||
<lang z80>org &1001 |
|||
ld hl,&1001 ;&1000 won't work since that contains &00. |
|||
ld e,&ff ;&00 is the terminator so we can't use zero as an immediate directly |
|||
inc e ;Must abuse 8-bit underflow to get it. |
|||
loop: |
|||
ld a,(hl) |
|||
cp e |
|||
jp z,ProgramEnd |
|||
call UnpackNibbles |
|||
ld a,b |
|||
cp &0A |
|||
jr c,noCorrectHex_B |
|||
add &07 |
|||
noCorrectHex_B: |
|||
add &30 |
|||
call &bb5a |
|||
ld a,c |
|||
cp &0A |
|||
jr c,noCorrectHex_C |
|||
add &07 |
|||
noCorrectHex_C: |
|||
add &30 |
|||
call &bb5a |
|||
inc hl |
|||
jp loop |
|||
ProgramEnd: |
|||
ld a,&30 |
|||
call &bb5a |
|||
ld a,&30 |
|||
call &bb5a |
|||
ret ;return to basic |
|||
UnpackNibbles: |
|||
;splits a into its component nibbles, storing high nibble in B and low in C. |
|||
push af |
|||
and &0f |
|||
ld c,a |
|||
pop af |
|||
and &f0 |
|||
rrca |
|||
rrca |
|||
rrca |
|||
rrca |
|||
ld b,a |
|||
ret |
|||
db 0 ;null terminator. No instructions or operands above compile to &00. </lang> |