I am trying to draw a pacman sprite in assembly, and I have a function that is working well, the code is here:
DEFINE_LINE EQU 600AH ; address of the command to define the line
DEFINE_COLUMN EQU 600CH ; address of the command to define the column
DEFINE_PIXEL EQU 6012H ; address of the command to write a pixel
CLEAR_NOTICE EQU 6040H ; address of the command to clear the "no scenario selected" notice
CLEAR_SCREEN EQU 6002H ; address of the command to clear all drawn pixels
SELECT_BACKGROUND_SCENARIO EQU 6042H ; address of the command to select a background image
; map limits
MIN_COLUMN EQU 0 ; number of the leftmost column the object can occupy
MAX_COLUMN EQU 63 ; number of the rightmost column the object can occupy
DELAY EQU 400H ; delay to limit the movement speed of the character
LINE EQU 14 ; character line (middle of the screen)
COLUMN EQU 31 ; character column (middle of the screen)
PACMAN_LENGTH EQU 4
PACMAN_HEIGHT EQU 5
WHITE EQU 0FFFFH ; white color
YELLOW EQU 0FFF0H
GREEN EQU 0F090H
BLUE EQU 0F36FH ; blue color
; *********************************************************************************
; * Data
; *********************************************************************************
PLACE 1000H
stack_:
STACK 100H ; space reserved for the stack
; (200H bytes, as they are 100H words)
initial_SP: ; this is the address (1200H) with which the SP must be
; initialized. The 1st return address will be stored
; at 11FEH (1200H-2)
DEF_PACMAN: ; table that defines the Pacman (color, width, pixels)
WORD PACMAN_LENGTH
WORD 0,YELLOW,YELLOW,0
WORD YELLOW,YELLOW,YELLOW,YELLOW
WORD YELLOW,0,0,0
WORD YELLOW,YELLOW,YELLOW,YELLOW
WORD 0,YELLOW,YELLOW,0
; *********************************************************************************
; * Code
; *********************************************************************************
PLACE 0 ; the code must start at 0000H
start:
MOV SP, initial_SP ; initializes SP to the word following
; the last one in the stack
MOV [CLEAR_NOTICE], R1 ; clears the "no scenario selected" notice (the value of R1 is not relevant)
MOV [CLEAR_SCREEN], R1 ; clears all drawn pixels (the value of R1 is not relevant)
MOV R1, 1 ; background scenario number 0
MOV [SELECT_BACKGROUND_SCENARIO], R1 ; selects the background scenario
MOV R1, LINE ; line where to start drawing the sprite
MOV R2, COLUMN ; column of where to start drawing the sprite
MOV R0,1 ; Set the draw lines to mode 1( mode 1 writes , mode 0 erases)
CALL draw_pacman
MOV R7,1
end:
JMP end ; end program
draw_pacman:
MOV R4, DEF_PACMAN
ADD R4, 2 ; address of the color of the 1st pixel (2 because the width is a word)
MOV R5, PACMAN_HEIGHT ; get character height
MOV R7,PACMAN_LENGTH; put the character length in R7
MOV R6,R7
MOV R8,COLUMN
CALL draw_lines
RET
change_line:
SUB R5,1 ; minus one line to treat
JZ stop ; if you have already gone through the entire height of the sprite, stop drawing.
ADD R1,1 ; change line
MOV R2,R8 ; reset column position
MOV R6,R7 ; reset loop
draw_lines:
CMP R0,0 ; check if it's in write mode
JNZ changecolor
; draw the character pixels from the table
MOV R3,0
JMP continue
changecolor:
MOV R3, [R4] ; get the color of the next character pixel
continue:
CALL write_pixel ; write each character pixel
ADD R4, 2 ; address of the color of the next pixel (2 because each pixel color is a word)
ADD R2, 1 ; next column
; minus one column to treat
SUB R6,1
JZ change_line
JNZ draw_lines ; continue until you cover the entire width of the object
stop:
RET
write_pixel:
MOV [DEFINE_LINE], R1 ; select the line
MOV [DEFINE_COLUMN], R2 ; select the column
MOV [DEFINE_PIXEL], R3 ; change the color of the pixel in the selected line and column
RET
The function draw_pacman sets R1 and R2 as the X and Y coordinates where we’ll start drawing the Pacman, and it starts reading the DEF_PACMAN pixel table , then draws the sprite line by line, changing line every 4 pixels. It works, but I would like to make the table with bytes like this:
DEF_PACMAN: ; table that defines the Pacman (color, width, pixels)
; 1 is supposed to be YELLOW, 0 transparent and 2 can either be transparent or yellow , changing from time to time on a loop (Pacman mouth opening and closing)
WORD PACMAN_LENGTH
WORD SCALE ; if i change the scale to 2 per example I want the pacman to double its size
BYTE 0,1,1,0
BYTE 1,1,1,1
BYTE 1,2,2,2
BYTE 1,1,1,1
BYTE 0,1,1,0
I know this is possible because I saw it in a YouTube video but I didn’t understand how I could implement this in my function. This is assembly code that is being run in a simulator PEPE-16. Any tips on optimization would also be appreciated.
enter code here