Assembler Example

From C64-Wiki
(Redirected from Assembler-Example)
Jump to: navigation, search

These 6502 assembler example programs demonstrate simple applications. To code these programs you will need a program that is called assembler monitor or machine code monitor. This monitor programs (like MONITOR$C000, MONITOR$8000 (Commodore)‎, TIM, SMON or Supermon 64) must load separate from disk or tape. Also you can use cartridges like the Final Cartridge 3 or Action Replay, which have a build-in machine code monitor.

Note that most monitor-programs will let you input plain assembler, while assembler- or macro-assembler-programs will let you also use labels and constants like shown here.


; This program rapidly updates the colors
; of the screen and the border.

*=$c000   ; starting address of the program 

BORDER = $d020
SCREEN = $d021

start   inc SCREEN  ; increase screen colour 
        inc BORDER  ; increase border colour
        jmp start   ; repeat

After some short compilation by the assembler-program you can run this by typing SYS 49152 from BASIC. (The number 49152 equals C000 hexadecimal, the starting address of this example code.)



Here's a simple PRINT subroutine you can include in your assembly language programs. Call it as follows:

        ldx #<string          ;string address least significant byte (LSB)
        ldy #>string          ;string address most significant byte (MSB)
        jsr sprint

string must point to a null-terminated character string.

bsout    =$ffd2                ;kernel character output sub
ptr      =$fb                  ;zero page pointer
;
sprint   stx ptr               ;save string pointer LSB
         sty ptr+1             ;save string pointer MSB
         ldy #0                ;starting string index
;
sprint01 lda (ptr),y           ;get a character
         beq sprint02          ;end of string
;
         jsr bsout             ;print character
         iny                   ;next
         bne sprint01
;
sprint02 rts                   ;exit

Self-modififying code version (this does not need a zero page pointer, but program needs to be in RAM - which is usually the case)

bsout    =$ffd2                ;kernel character output sub
;
sprint   stx sprint01+1        ;save string pointer LSB
         sty sprint01+2        ;save string pointer MSB
         ldy #0                ;starting string index
;
sprint01 lda $1000,y           ;get a character
         beq sprint02          ;end of string
;
         jsr bsout             ;print character
         iny                   ;next
         bne sprint01
;
sprint02 rts                   ;exit