Output a String to Console (BBG-DOS) using 6502 Assembly for 8-bit Famicom Clone BBG – Tutorial 4


In 16-bit MSDOS, you can create small executable files that end the filename with ‘.COM’. The .COM is small and less than 64 KB in total file size. There is no header so code and data is continuously held in file. There is a 256Byte block information reserved by operating system. It contains the command line parameters etc. Therefore, for .COM files, it is placed at offset $100 (with the assembly keyword org). At Microsoft Assembler, the .model tiny will tell the compiler to generate .COM programs. Since .COM programs are easy to load and small in file size, they are often victims for the virus or malicious programs.

You can create .COM files directly using DEBUG.exe and the following shows how easy to create binary executables using 16-bit assembly. The ‘Hello.COM’ created is only 20 bytes. It prints the message ‘Hello!’ to DOS console and returns.

8677a3c48030520a0395fdd810fa9d96.jpg Output a String to Console (BBG-DOS) using 6502 Assembly for 8-bit Famicom Clone BBG - Tutorial 4 16 bit 6502 8 bit assembly language code code library compiler console famicom implementation Nintendo Entertainment System programming languages

The MSDOS interrupts 21h (e.g. int 21h) is for DOS functions. The function 9 prints a string that ends with dollar sign $. The function 0x4c returns to the OS and the error code is stored at register al. The message location is stored at 16-bit register dx. The 8-bit register ah is normally used to specify which DOS function.

On 8-bit BBG Famiclone, we have a DOS-clone, BBGDOS, but it is only 8-bit. Previously, we have showed the 8-bit assembly code to print a single character to BBGDOS. The 8-bit assembly (6502) code is directly compiled at BBGDOS command prompt using basm.cmd assembler. The basm.cmd takes /cmd to generate .cmd programs or /obj option to generate .obj which is to be linked into bigger program .exc (similar to .EXE on MSDOS)

To print a message using 6502 assembly code in BBGDOS, we will need to call the BBGDOS functions (there is no interrupts in 6502 assembly, so we will use jsr to jump to subroutines). The function 4 is reserved for printing a message, which also ends with the dollar sign $.

DosDisplayString   equ 4

The DOSIOEntry is a constant that is defined in the include file xnrdef.h. It specifies the entry of the DOS function calls.

DOSIOEntry   equ $580c

We can then define a string using db which is the same for most assembly programming platforms such as 16-bit MSDOS, 32-bit Windows..

msg db "Hello!$"

Before calling DosDisplayString we need to give the address of the string to register X and Y which stores the low and high address respectively.

ldx #<msg
ldy #>msg

We use the jsr DOSIOEntry to print that message.

To return to BBGDOS, we can call 0 function.

DosProgramReturn   equ 0

The return code is set in register X and the register Y, when bit 1 set, the BBGDOS will print ‘Return from running program’ when exiting the program.

Therefore, the entire 6502 assembly for BBG Famiclone is (pretty self-explanatory even without comments) :

; HELLOACM.COM
include xnrdef.h
org $8000
jmp start
msg: db "Hello!", 13, 10, "$"
start:
    ldx #<msg
    ldy #>msg
    lda #DosDisplayString
    jsr DOSIOEntry
    ldx #$0
    ldy #$0
    lda #DosProgramReturn
    jmp DOSIOEntry

e834cb801ef7a45a11a49d596ba04929.jpg Output a String to Console (BBG-DOS) using 6502 Assembly for 8-bit Famicom Clone BBG - Tutorial 4 16 bit 6502 8 bit assembly language code code library compiler console famicom implementation Nintendo Entertainment System programming languages

You can save above code in hello.asm using edit.cmd or wps.exc text editor, and then use basm /cmd hello.asm to compile to hello.cmd binary executable for BBGDOS. The alternative is to directly put 6502 assembly code in the debug.cmd (8 bit debugger for BBGDOS).

691536e56e8b02f613ad3e6b0ad35617.jpg Output a String to Console (BBG-DOS) using 6502 Assembly for 8-bit Famicom Clone BBG - Tutorial 4 16 bit 6502 8 bit assembly language code code library compiler console famicom implementation Nintendo Entertainment System programming languages

The hello.cmd is 32-bytes only, which is larger than 20-byte hello.com. Note that the .cmd has a two byte offset in the beginning of the file telling the BBGDOS where to load the .CMD program.

d6c6c96e30ba0c193826cb81e5a57bc9.jpg Output a String to Console (BBG-DOS) using 6502 Assembly for 8-bit Famicom Clone BBG - Tutorial 4 16 bit 6502 8 bit assembly language code code library compiler console famicom implementation Nintendo Entertainment System programming languages

Hello.cmd runs both on BBGDOS (English) and BBGCDOS (Chinese).

Assembly rocks even for 8-bit!

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
955 words
Last Post: Storing a number inside a string using PHP
Next Post: Print 26 Uppercase Letters using 6502 Assembler on 8-bit Famicom Clone BBG (BBK) - Tutorial 5 - Using Loop

The Permanent URL is: Output a String to Console (BBG-DOS) using 6502 Assembly for 8-bit Famicom Clone BBG – Tutorial 4

Leave a Reply