Print 26 Uppercase Letters using 6502 Assembler on 8-bit Famicom Clone BBG (BBK) – Tutorial 5 – Using Loop


On 16-bit MSDOS, you can use debug.exe (16-bit debugger) to write tiny (or small) .COM binary programs. There is no compilation process, you write the assembly code and the debugger will translate immediately into machine code. The .COM files are binary program dumps with program entry at offset 0x100 (256 bytes known as the PSP (Program Segment Prefix), which contains information about the process) and everything (data and code) is stored inside a whole (64KB maximum) file. There is no clear section reserved for data, so you can design the whole 64kb space on your own (with lots of jumping instructions such as jmp, jne etc).

The following code has only size 15 Byte and it prints 26 upper case letters to console and return to the OS using int 0x20 (this requires the CS point to the PSP, and in .COM files, it is true otherwise suggested to use 0x4c function to return to DOS with the return code).

e363061999333bbaafc92c2b972dc20a.jpg Print 26 Uppercase Letters using 6502 Assembler on 8-bit Famicom Clone BBG (BBK) - Tutorial 5 - Using Loop 16 bit 6502 8 bit assembly language code code library compiler console debug famicom hardware implementation interpreter / compiler Nintendo Entertainment System programming languages

Of course, we can use 09 function to print the 26 letters by pointing the register DX to the predefined string (that ends with dollar sign $) but the point here is to illustrate the loop design. In 16-bit MSDOS, we use AH=2 to print a single character (the character is stored in 8-bit register DL). We assign the loop counter in the 16-bit register CX and use LOOP will decrement the CX first and check if it is zero, then continue otherwise jump to the specified location. Note that everytime int is called (interrupt), the register AX is changed, so you have to reassign the value to AH or preserve it using stacks (push and pop)

On 8-bit BBG Famicom clone, you can run 8-bit assembler basm.cmd on 8-bit BBGDOS.

The following 8-bit 6502 assembly code compiles using 8-bit BBG Assembler basm.cmd and it will generate a ascii.cmd which is 42 bytes. Because 6502 instructions are less than 16-bit assembly instructions, so normally it takes longer and more code to achieve the same functionality.

181ba2d5b85fb6d6994cb77e028cbb2a.jpg Print 26 Uppercase Letters using 6502 Assembler on 8-bit Famicom Clone BBG (BBK) - Tutorial 5 - Using Loop 16 bit 6502 8 bit assembly language code code library compiler console debug famicom hardware implementation interpreter / compiler Nintendo Entertainment System programming languages

; HelloACM.com
include xnrdef.h
org $8000
jmp start
chr    db  "A"
cnt    db  26
start:
    ldx chr
    ldy cnt
loop:
    lda #DosDisplayChar
    jsr DosIOEntry
    inc chr   ; next character
    ldx chr   ; load it from memory to register x
    dec cnt   ; decrement the number of characters to print
    lda cnt   ; load the counter
    bne loop  ; if not zero, jmp to loop
    ldx #$0
    ldy #$0
    lda #DosProgramReturn
    jmp DosIOEntry

There are less registers in 6502 assembly, the most commonly-used are A, X and Y. Therefore, we cannot hold every data in registers since they will be overwritten from time to time. We declare using DB two byte variables held in memory the counter (26 characters) and the character ASCII code.

We use bne instruction to check if the flag register bit ‘zero’ is not set, there is a cmp #$0 before bne loop but we can omit it because the number we compare to is zero.

Save the above code into ascii.asm using edit.cmd or wps.exc (text editors) and then use basm /cmd ascii.asm to compile the above code into ascii.cmd with the generated map file ascii.map.

cdad7145554ba93c08c28492f39b4982.jpg Print 26 Uppercase Letters using 6502 Assembler on 8-bit Famicom Clone BBG (BBK) - Tutorial 5 - Using Loop 16 bit 6502 8 bit assembly language code code library compiler console debug famicom hardware implementation interpreter / compiler Nintendo Entertainment System programming languages

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
811 words
Last Post: Output a String to Console (BBG-DOS) using 6502 Assembly for 8-bit Famicom Clone BBG - Tutorial 4
Next Post: Use Pixel/Grid Walking Simulation to Fill a Spiral Matrix

The Permanent URL is: Print 26 Uppercase Letters using 6502 Assembler on 8-bit Famicom Clone BBG (BBK) – Tutorial 5 – Using Loop

5 Comments

  1. Peter
  2. Peter

Leave a Reply