Skip to main content
  1. Projects/

6809 Assembly

<time datetime="2023-07-10 22:46:30 &#43;1200 &#43;1200">10 July 2023</time><span class="px-2 text-primary-500">&middot;</span><span title="Reading time">10 mins</span><span class="px-2 text-primary-500">&middot;</span><span> <span id="views_projects/6809-assembly/index.en.md" title="views">0</span> <span class="inline-block align-text-bottom"> <span class="relative block icon"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"> <path fill="currentColor" d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg> </span> </span> </span><span class="px-2 text-primary-500">&middot;</span><span> <span id="likes_projects/6809-assembly/index.en.md" title="likes">0</span> <span class="inline-block align-text-bottom"> <span class="relative block icon"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"> <path fill="currentColor" d="M47.6 300.4L228.3 469.1c7.5 7 17.4 10.9 27.7 10.9s20.2-3.9 27.7-10.9L464.4 300.4c30.4-28.3 47.6-68 47.6-109.5v-5.8c0-69.9-50.5-129.5-119.4-141C347 36.5 300.6 51.4 268 84L256 96 244 84c-32.6-32.6-79-47.5-124.6-39.9C50.5 55.6 0 115.2 0 185.1v5.8c0 41.5 17.2 81.2 47.6 109.5z"/></svg> </span> </span> </span><span class="px-2 text-primary-500">&middot;</span><span> <button id="likes_button" class="rounded-md border border-primary-400 px-1 py-[1px] text-xs font-normal text-primary-700 dark:border-primary-600 dark:text-primary-400" onclick="process_article()"> <span id="likes_button_heart" style="display:none" class="inline-block align-text-bottom"> <span class="relative block icon"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"> <path fill="currentColor" d="M47.6 300.4L228.3 469.1c7.5 7 17.4 10.9 27.7 10.9s20.2-3.9 27.7-10.9L464.4 300.4c30.4-28.3 47.6-68 47.6-109.5v-5.8c0-69.9-50.5-129.5-119.4-141C347 36.5 300.6 51.4 268 84L256 96 244 84c-32.6-32.6-79-47.5-124.6-39.9C50.5 55.6 0 115.2 0 185.1v5.8c0 41.5 17.2 81.2 47.6 109.5z"/></svg> </span> </span> <span id="likes_button_emtpty_heart" class="inline-block align-text-bottom"> <span class="relative block icon"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"> <path fill="currentColor" d="M244 84L255.1 96L267.1 84.02C300.6 51.37 347 36.51 392.6 44.1C461.5 55.58 512 115.2 512 185.1V190.9C512 232.4 494.8 272.1 464.4 300.4L283.7 469.1C276.2 476.1 266.3 480 256 480C245.7 480 235.8 476.1 228.3 469.1L47.59 300.4C17.23 272.1 0 232.4 0 190.9V185.1C0 115.2 50.52 55.58 119.4 44.1C164.1 36.51 211.4 51.37 244 84C243.1 84 244 84.01 244 84L244 84zM255.1 163.9L210.1 117.1C188.4 96.28 157.6 86.4 127.3 91.44C81.55 99.07 48 138.7 48 185.1V190.9C48 219.1 59.71 246.1 80.34 265.3L256 429.3L431.7 265.3C452.3 246.1 464 219.1 464 190.9V185.1C464 138.7 430.4 99.07 384.7 91.44C354.4 86.4 323.6 96.28 301.9 117.1L255.1 163.9z"/></svg> </span> </span> <span id="likes_button_text">&nbsp;Like</span> </button> </span><span class="px-2 text-primary-500">&middot;</span> <span class="mb-[2px]"> <a href="https://github.com/Anoxiacxy/hugo-blog/tree/main/content/projects/6809-assembly/index.en.md" class="text-lg hover:text-primary-500" rel="noopener noreferrer" target="_blank" title="Edit content" ><span class="inline-block align-text-bottom"> <span class="relative block icon"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M490.3 40.4C512.2 62.27 512.2 97.73 490.3 119.6L460.3 149.7L362.3 51.72L392.4 21.66C414.3-.2135 449.7-.2135 471.6 21.66L490.3 40.4zM172.4 241.7L339.7 74.34L437.7 172.3L270.3 339.6C264.2 345.8 256.7 350.4 248.4 353.2L159.6 382.8C150.1 385.6 141.5 383.4 135 376.1C128.6 370.5 126.4 361 129.2 352.4L158.8 263.6C161.6 255.3 166.2 247.8 172.4 241.7V241.7zM192 63.1C209.7 63.1 224 78.33 224 95.1C224 113.7 209.7 127.1 192 127.1H96C78.33 127.1 64 142.3 64 159.1V416C64 433.7 78.33 448 96 448H352C369.7 448 384 433.7 384 416V319.1C384 302.3 398.3 287.1 416 287.1C433.7 287.1 448 302.3 448 319.1V416C448 469 405 512 352 512H96C42.98 512 0 469 0 416V159.1C0 106.1 42.98 63.1 96 63.1H192z"/></svg> </span> </span></a > </span>
link
Author
Link
Game Developer & Artifical Intelligence Engineer
Table of Contents

What is 6089? #

Well, the last time I was confused like this was when I seen the number 6502. I guess it is a microproc

jimbro1000/online6809

Online Motorola 6809 Emulator

HTML
6
4

Let’s do some assembly #

The best way to learn is to do. So let’s do some assembly. I am going to use the online 6809 assembler. And try to write programs as below.

Count From 0 To 9 #

Write a program that prints the digits from 0 to 9, one after another, in the top left of the text screen. That is, it prints a 0 in the top left, then it replaces the 0 with a 1, then it replaces the 1 with a 2, and so on until it has printed 9. It should return to the operating system (with an RTS) at the end of the program.

const text_screen = $0400

start:
    ldd #$0000      ; load the value 0                  | 
    std var         ; store the value 0 in var          | var = 0
loop:               ;                                   | do {
    ldd var         ; load the value of var             | 
    pshs d          ; save d                            | 
    jsr PrintDigit  ; print the value of var            | print(var)
    puls d          ; remove d from the stack           |

    ldd var         ; load the value of var             |
    addd #$0001     ; add 1 to the value of var,        | var += 1
    std var         ; store the new value of var        |
    
    cmpd #$000A     ; compare with the value 9 + 1 = A  | 
    bne loop        ; if not equal, go to loop          | } while (var != 10)
    rts             ; return from subroutine            | return
var:
    .word 0         ; reserve 2 bytes for the variable

PrintDigit:         ; This function prints the value in the stack
                    ; at the address of the user stack pointer u + 4, 
                    ; pshs <a>: push any register <a> to the stack 
    pshs u          ; save u, s = s - 2                 | 
    leau ,s         ; u = s                             | PrintDigit(int x) {
    
    ldd $04,u       ; d = mem[u + 4]                    | d = x
    addd #'0'       ; d = d + '0'                       | d += '0'
    stb text_screen ; mem[text_screen] = b              | print(d)

    leas ,u         ; s = u                             | }
    puls u          ; load u
    rts

To get the extra marks, your program must be 8 or fewer instructions in length.

start:
    lda #'0'    ; load the value 0                  | a = '0'
loop:                                               | do {
    sta $0400   ; print a                           |   print(a)
    inca        ; a = a + 1                         |   a = a + 1
    cmpa #':'   ; compare with the value '9' + 1    | 
    bne loop    ; if not equal, go to loop          | } while (a != '9' + 1)
    rts         ; return from subroutine            | return

Single Digit Fibonacci #

Write a program to compute and print each of the first 7 Fibonacci numbers. It must compute the numbers in the sequence. It must print each, one after the other, in the top left of the text screen in the emulator. The first 7 numbers in the sequence are: 0, 1, 1, 2, 3, 5, 8. These are all single digit numbers.

const text_screen = $0400

Start:  
    ldd #$0007          ; d = 7
    pshs d              ; save d
    lbsr Fibonacci      ; call Fibonacci
    puls d              ; load d
    rts

Fibonacci:              ; fib requires a parameter in a
    pshs u              ; mem[--s] = u
    leau ,s             ; u = s

    ldd #0              ; d = 0
    pshs d              ; mem[--s] = d          def a = 0
    ldd #1              ; d = 1
    pshs d              ; mem[--s] = d          def b = 1
fib_loop:
    ldd $04, u          ; d = mem[u + 4]
    cmpd #0             ; if d = 0              if n = 0
    beq fib_done        ; return 0
    subd #1             ; d = d - 1 
    std $04,u           ; mem[u + 4] = d        n = n - 1

    ldd $02,s           ; d = mem[s + 2]
    pshs d              ; mem[--s] = d          
    lbsr PrintDigit     ; call PrintDigit
    puls d              ; d = mem[s++]

    ldd ,s              ; d = mem[s]
    addd $02,s          ; d = mem[s + 2]        a = mem[s] + mem[s + 2]
    ldx ,s              ; x = mem[s]            x = mem[s]
    std ,s              ; mem[s] = d            mem[s] = a
    stx $02,s           ; mem[s + 2] = x        mem[s + 2] = x

    bra fib_loop        ; goto fib_loop
fib_done:
    leas ,u             ; s = u
    puls u              ; u = mem[s++]
    rts                 ; return

PrintDigit:
    pshs u              ; save u
    leau ,s             ; u = s
    
    ldd $04,u           ; d = mem[u + 4]
    addd #'0'           ; d = d + '0'
    stb text_screen     ; mem[text_screen] = b

    leas ,u             ; s = u
    puls u              ; load u
    rts

Extend your program to print the numbers one after the other on the top row of the text screen. That is, the top left corner of the screen should read “0112358” once your program has finished running.

const text_screen = $0400

Start:  
    ldx #text_screen    ; x = text_screen
    stx cursor          ; cursor = x
    ldd #$0007          ; d = 7
    pshs d              ; save d
    lbsr Fibonacci      ; call Fibonacci
    puls d              ; load d
    rts

Fibonacci:              ; fib requires a parameter in a
    pshs u              ; mem[--s] = u
    leau ,s             ; u = s

    ldd #0              ; d = 0
    pshs d              ; mem[--s] = d          def a = 0
    ldd #1              ; d = 1
    pshs d              ; mem[--s] = d          def b = 1
fib_loop:
    ldd $04, u          ; d = mem[u + 4]
    cmpd #0             ; if d = 0              if n = 0
    beq fib_done        ; return 0
    subd #1             ; d = d - 1 
    std $04,u           ; mem[u + 4] = d        n = n - 1

    ldd $02,s           ; d = mem[s + 2]
    pshs d              ; mem[--s] = d          
    lbsr PrintDigit     ; call PrintDigit
    puls d              ; d = mem[s++]

    ldd ,s              ; d = mem[s]
    addd $02,s          ; d = mem[s + 2]        a = mem[s] + mem[s + 2]
    ldx ,s              ; x = mem[s]            x = mem[s]
    std ,s              ; mem[s] = d            mem[s] = a
    stx $02,s           ; mem[s + 2] = x        mem[s + 2] = x

    bra fib_loop        ; goto fib_loop
fib_done:
    leas ,u             ; s = u
    puls u              ; u = mem[s++]
    rts                 ; return

PrintDigit:
    pshs u              ; save u
    leau ,s             ; u = s
    
    ldd $04,u           ; d = mem[u + 4]
    addd #'0'           ; d = d + '0'
    ldx cursor          ; x = cursor
    stb ,x+             ; mem[x + 1] = d
    stx cursor          ; cursor = x
    
    leas ,u             ; s = u
    puls u              ; load u
    rts
cursor:
    .word 0             ; cursor = 0

Hexadecimal Fibonacci #

Write a routine that, given a value in register D, will print the value in hexadecimal. It should print green on a black background. Hint: The emulator screen does not use ASCII, character 1 is ‘A’, character 2 is ‘B’, and so on. For this you might find it useful to write a routine that given a character in register A, will print it to the screen. With these routines, write a short test program to print the value FDBC then a space then 1234. The top left of the screen should read “FDBC 1234” once you have completed this.

const text_screen = $0400

Start:
	ldx #text_screen    ; 
	stx cursor          ; cursor = text_screen
	ldd #$FDCB          ; 
	lbsr PrintHex       ; PrintHex(d = FDCB)
	ldd #space		    ; 
	lbsr PrintStr       ; PrintStr(d = space)
	ldd #$1234          ; 
	lbsr PrintHex       ; PrintHex(d = 1234)
	rts

PrintStr:
	; the string is in the accumulator D
	pshs y              ; save y
	ldx cursor          ; x = cursor
	
	tfr d,y             ; y = d
print_str_loop:
	lda ,y+             ; a = mem[y++]
	cmpa #0             ; if (a == 0)
	beq print_str_done  ;   goto print_str_done
	sta ,x+             ; mem[x++] = a
	bra print_str_loop  ; goto print_str_loop
print_str_done:
	stx cursor          ; cursor = x
	puls y              ; restore y
	rts

PrintHex:
	; the number is in the accumulator D
	pshs y              ; save y
	ldx cursor          ; x = cursor

	tfr d,y             ; y = d
	lsra                
	lsra                 
	lsra
	lsra                ; a = y >> 8 >> 4
	bsr DigitOptimized  
	tfr y,d
	anda #$0F           ; a = y >> 8 & 0x0F
	bsr DigitOptimized  
	lsrb
	lsrb
	lsrb
	lsrb
	tfr b,a             ; a = y >> 4 & 0x0F
	bsr DigitOptimized
	tfr y,d
	andb #$0F
	tfr b,a             ; a = y & 0x0F
	bsr DigitOptimized  

	stx cursor
	puls y
	rts

DigitOptimized:
    ; the digit is in the accumulator A
	; make sure: 
	; - ldx cursor before
	; - sdx cursor after
	cmpa #$0A           ; if (a >= 10)
	blo if_digit	    ;   goto if-digit
	suba #$9	    ; 
	bra if_merge	    ; goto if-merge
if_digit:
    	adda #'0'		; a += '0'
if_merge:
    	sta ,x+			; mem[x + 1] = d
    	rts

cursor:
    	.word 0			; cursor = 0

space:
	.byte ' ', 0

Write a program that prints a comma seperated list of the first 25 Fibonacci numbers in hexadecimal. You must compute the sequence. Once your program has return the text screen should look like this:

Fibonacci
const text_screen = $0400

Start:
    ldx #text_screen    ; x = text_screen
    stx cursor          ; cursor = x
    ldd #25             ; d = 25
    pshs d              ; mem[--s] = d
    lbsr Fibonacci      ; call Fibonacci
    puls d              ; d = mem[s++]
    rts

Fibonacci:              ; fib requires a parameter in a
    pshs u              ; mem[--s] = u
    leau ,s             ; u = s

    ldd #0              ; d = 0
    pshs d              ; mem[--s] = d          def a = 0
    ldd #1              ; d = 1
    pshs d              ; mem[--s] = d          def b = 1
fib_loop:
    ldd $04, u          ; d = mem[u + 4]
    cmpd #0             ; if d = 0              if n = 0
    beq fib_done        ; return 0
    subd #1             ; d = d - 1 
    std $04,u           ; mem[u + 4] = d        n = n - 1

    ldd $02,s           ; d = mem[s + 2]
    lbsr PrintHex       ; call PrintHex

    ldd $04,u           ; d = mem[u + 4]
    cmpd #0             ; if d = 0              if n = 0
    beq skip_comma      ; goto skip_comma
    ldd #comma          ; d = comma
    lbsr PrintStr       ; call PrintStr
skip_comma:
    ldd ,s              ; d = mem[s]
    addd $02,s          ; d = mem[s + 2]        a = mem[s] + mem[s + 2]
    ldx ,s              ; x = mem[s]            x = mem[s]
    std ,s              ; mem[s] = d            mem[s] = a
    stx $02,s           ; mem[s + 2] = x        mem[s + 2] = x

    bra fib_loop        ; goto fib_loop
fib_done:
    leas ,u             ; s = u
    puls u              ; u = mem[s++]
    rts                 ; return

PrintStr:
    ; the string is in the accumulator D
    pshs y              ; save y
    ldx cursor          ; x = cursor
    
    tfr d,y             ; y = d
print_str_loop:
    lda ,y+             ; a = mem[y++]
    cmpa #0             ; if (a == 0)
    beq print_str_done  ;   goto print_str_done
    sta ,x+             ; mem[x++] = a
    bra print_str_loop  ; goto print_str_loop
print_str_done:
    stx cursor          ; cursor = x
    puls y              ; restore y
    rts

PrintHex:
    ; the number is in the accumulator D
    pshs y              ; save y
    ldx cursor          ; x = cursor

    tfr d,y             ; y = d
    lsra
    lsra
    lsra
    lsra                ; a = y >> 8 >> 4
    bsr DigitOptimized
    tfr y,d
    anda #$0F           ; a = y >> 8 & 0x0F
    bsr DigitOptimized
    lsrb
    lsrb
    lsrb
    lsrb
    tfr b,a             ; a = y >> 4 & 0x0F
    bsr DigitOptimized
    tfr y,d
    andb #$0F
    tfr b,a             ; a = y & 0x0F
    bsr DigitOptimized

    stx cursor
    puls y
    rts

DigitOptimized:
    ; the digit is in the accumulator A
    ; make sure: 
    ; - ldx cursor before
    ; - sdx cursor after
    cmpa #$0A           ; if (a >= 10)
    blo if_digit		;   goto if-digit
    suba #$9			; 
    bra if_merge		; goto if-merge
if_digit:
    adda #'0'           ; a += '0'
if_merge:
    sta ,x+             ; mem[x + 1] = d
    rts

cursor:
    .word 0             ; cursor = 0

space:
    .byte ' ', 0
comma:
    .byte ',', 0

Download #

Warning! The source code is stored in the web site, not in a database.
  1. Download the source code.
Download code.zip
  1. Extract the zip file.
unzip code.zip