Jump to content

The obligatory "Hello, World!"


Recommended Posts

I like it. Couple of bugs, though:

  • ldx, but indexing on y -> No idea what offset you'll be grabbing
  • Never increments the index register -> Infinite loop

Might also be considered a bug that the string length is limited to 256 characters.

Here's a few alternatives.

Arbitrary-length, null-terminated string (for up to a 16-bit address space):

	CHROUT = $FFD2
	lda #<Msg
	sta loop+1
	lda #>Msg
	sta loop+2
	loop: lda Msg
	beq end
	jsr CHROUT
	inc loop+1
	bne loop
	inc loop+2
	bra loop
	end: rts
	Msg: .byte "Hello, World!", 0
	

String length less than or equal to 256 characters, null-terminated:

	CHROUT = $FFD2
	ldy #0
	loop: lda Msg,y
	beq end
	jsr CHROUT
	iny
	bne loop
	end: rts
	Msg: .byte "Hello, World!", 0
	

String length less than or equal to 256 characters, length-prefixed:

	CHROUT = $FFD2
	ldy #0
	ldx Msg
	loop: lda Msg+1,y
	jsr CHROUT
	iny
	dex
	bne loop
	rts
	Msg: .byte $0d, "Hello, World!"
	

  • Thanks 1
Link to comment
Share on other sites

Using BNE instead of BRA or JMP is a way to automatically terminate printing if the string ever goes above 256 characters.

Once X contains $FF (index of the 256th character), and the INX is executed, the value in the register will wrap back to $00. The Z flag in the processor status register is set to indicate a result of zero. Since BNE branches only if Z is cleared, the branch never sends the program back to "Loop" to repeat for any more characters, and falls down to the RTS.

If you didn't use BNE, then printing a string that's 256 or more characters long will cause the loop to never end, repeatedly outputting the first 256 characters over and over. Unless the interrupt handler somehow modifies the string so that a terminator ($00) is present, you have to reset the machine.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

Please review our Terms of Use