Jump to content
JimmyDansbo

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

Share this post


Link to post
Share on other sites

D'oh. I knew I should not have started playing around with switching options and so on before creating the image.

Here is version 0.2

image.thumb.png.87a41ef5b7b57d5db1d04e773849233a.png

The full size image has also been updated.

Maybe I should create a T-shirt from the original image, wear it and see how many will comment on the incorrectness 😛

 

Share this post


Link to post
Share on other sites

slithymatt micro-optimizing a t shirt/desktop picture 😉    Then again Jimmy's version is compatible with 6502 systems so there's that!

Edited by desertfish
  • Like 1

Share this post


Link to post
Share on other sites

I did not go for BRA because I wanted it to be 6502 compatible, but I did not consider that BNE could be used instead 😄

I might change the picture again later today.

Edited by JimmyDansbo
Changed 6202 to 6502, I don't know 6202 processor

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
1 hour ago, StinkerB06 said:

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

Good point!

Share this post


Link to post
Share on other sites

And if you're willing to reverse your strings and prefix them with their length, you can make a really fast routine:

	CHROUT = $FFD2
	ldx Msg
	loop: lda Msg, x
	jsr CHROUT
	dex
	bne loop
	rts
	Msg: .byte $0d, "!dlroW ,olleH"
	

Share this post


Link to post
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.


×
×
  • Create New...

Important Information

Please review our Terms of Use