Using a look to make the Hello World more compact!
The first version of the Hello World program in bare metal 65C02 assembler was 342 bytes long. That's quite a lot for such a simple assembler program. We can certainly do better. Since the RAM is not yet fitted subroutines are not yet an option. But we can do loops:
.pc02
.listbytes 16
.pagelength 66
.case -
.macpack generic
.include "VIA.inc"
.include "LCD.inc"
.segment "RODATA"
Message: .byte "Hello World!"
Message_Len = * - Message
.segment "CODE"
Do_RES: LDX #$FF
TXS
Set_A #%11100000 ; Set top 3 pin as output
Set_B #%11111111 ; Set all pins as output
Control_LCD #%00111000 ; Set 8-bit mode; 2 line display; 5×8 font
Control_LCD #%00001110 ; Display in; cursor on; blink off
Control_LCD #%00000110 ; Increment and shift cursor; don't shift display
Control_LCD #%00000001 ; Clear Display
LDX #$00
Loop: Data_LCD {Message,X} ; Write next character to Display
INX
CPX #(Message_Len) ; Repeat loop until X ≥ message lenght
BLT Loop
STP ; Stop Processor
Do_NMI: RTI
Do_IRQ: RTI
.segment "HEADER"
.word Do_NMI
.word Do_RES
.word Do_IRQ
A lot shorter. So what did I do?
- Moved the macros for the VIA and LCD out into include files. That way I don't need to repeat them in every new iteration of the program.
- Cleared the display. Without the text will not appear right at the beginning of the display. Do compare with the previous blog and see the difference.
- Stored the text as string in the RODATA (read only data) segment. This segment is written to the ROM after the CODE segment but before the HEADER segment.
- Replaced the repeated Data_LCD calls with a loop using the index register X as index to the current character. I used a simple count up loop as it's not that difficult to have the assembler calculate the length of the string instead of using a terminator character.
The result: The program is down to 135 bytes.
You find the source code for the program with makefile, linker configuration file, include files and assembler source code on GitLab: 6502Tutorial — Tools/Create_LED