sd:part_ii_writing_games_in_assembly_language
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| sd:part_ii_writing_games_in_assembly_language [2026/04/02 19:40] – appledog | sd:part_ii_writing_games_in_assembly_language [2026/04/03 05:30] (current) – appledog | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| == Introduction | == Introduction | ||
| - | At the end of [[SD-8516 Stellar BASIC]] we wrote a game called [[sdb: | + | At the end of [[SD-8516 Stellar BASIC]] we wrote a game called [[sdb: |
| == Part 1: Assembling and Running a Program | == Part 1: Assembling and Running a Program | ||
| - | An assembly language program should start with an .address | + | Just like BASIC programs have line numbers, or C programs have #include statements, an assembly language program should start with an '' |
| <codify armasm> | <codify armasm> | ||
| Line 122: | Line 122: | ||
| === draw_map | === draw_map | ||
| - | ==== VSTOP and CLS | ||
| Like in the BASIC version, the '' | Like in the BASIC version, the '' | ||
| Line 142: | Line 141: | ||
| Secondly, INT $10 AH=$10 is the clear screen function. We can call that as an interrupt helper here. | Secondly, INT $10 AH=$10 is the clear screen function. We can call that as an interrupt helper here. | ||
| - | ==== draw border | + | ==== Drawing the border |
| To draw the border we need to set up a loop that draws stars on the top and bottom, left and right, just like in BASIC. Here we will examine the top and bottom loop: | To draw the border we need to set up a loop that draws stars on the top and bottom, left and right, just like in BASIC. Here we will examine the top and bottom loop: | ||
| Line 169: | Line 168: | ||
| Here's the loop: After the top and bottom characters are drawn, we INC XL (which is the column marker). Then we CMP XL, #40. This means "check if it's under 40"-- i.e. 0-39. If we are on 39, we need to draw again. If we just drew on column 39 and then we INC XL to 40, that means don't continue the loop, and we fall-through to the next function; drawing on the left and right sides. | Here's the loop: After the top and bottom characters are drawn, we INC XL (which is the column marker). Then we CMP XL, #40. This means "check if it's under 40"-- i.e. 0-39. If we are on 39, we need to draw again. If we just drew on column 39 and then we INC XL to 40, that means don't continue the loop, and we fall-through to the next function; drawing on the left and right sides. | ||
| - | ==== draw border left and right | ||
| <codify armasm> | <codify armasm> | ||
| ; Draw left/right border: ' | ; Draw left/right border: ' | ||
| Line 190: | Line 188: | ||
| This code works just like the top and bottom but on the left and right sides. Instead of looping from 0 to 39 on XL, we will loop from 0 to 24 on YL. As before, we CMP to see if YL is 25. If it is, we fall through. A simple loop, in essence the exact same loop technique we used to draw the map in BASIC. | This code works just like the top and bottom but on the left and right sides. Instead of looping from 0 to 39 on XL, we will loop from 0 to 24 on YL. As before, we CMP to see if YL is 25. If it is, we fall through. A simple loop, in essence the exact same loop technique we used to draw the map in BASIC. | ||
| - | ==== draw player and robots | + | ==== Draw the player and robots |
| <codify armasm> | <codify armasm> | ||
| ; Draw player ' | ; Draw player ' | ||
| Line 218: | Line 216: | ||
| more to come... | more to come... | ||
| + | |||
| + | == Part 3: Player Input | ||
| + | Let's begin part 3 by adding a new subroutine to our main loop. Our main loop now becomes: | ||
| + | |||
| + | <codify armasm> | ||
| + | main_loop: | ||
| + | CALL @draw_map | ||
| + | CALL @get_input | ||
| + | RET | ||
| + | </ | ||
| + | |||
| + | === get_input | ||
| + | The primary goal of this section is to get input from the player and deal with it. We can do this by using the getkey function: | ||
| + | |||
| + | <codify armasm> | ||
| + | get_input: | ||
| + | LDAH $02 ; getkey() | ||
| + | INT $10 | ||
| + | </ | ||
| + | |||
| + | In this case, the function specification for AH=$02, INT 0x10 is: | ||
| + | |||
| + | ; ============================================================================ | ||
| + | ; AH=02h - Read Character (Blocking) | ||
| + | ; Input: | ||
| + | ; Output: AL = ASCII character | ||
| + | ; | ||
| + | ; | ||
| + | ; Note: X, Y preserved for cursor position | ||
| + | ; This function BLOCKS until a key is pressed | ||
| + | ; ============================================================================ | ||
| + | |||
| + | As we can see, this function returns the ASCII code of the key the player will press in '' | ||
| + | |||
| + | === uppercase or lowercase? | ||
| + | The player might type an uppercase command or a lowercase command. Let's normalize the keys to uppercase before processing them: | ||
| + | |||
| + | <codify armasm> | ||
| + | |||
| + | ; Uppercase: if AL >= ' | ||
| + | CMP AL, #97 | ||
| + | JNC @gi_check_keys | ||
| + | |||
| + | CMP AL, #123 | ||
| + | JC @gi_check_keys | ||
| + | | ||
| + | SUB AL, #32 | ||
| + | | ||
| + | gi_check_keys: | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | CMP works as follows: | ||
| + | |||
| + | CMP A, B ; if A >= B, then set carry | ||
| + | |||
| + | Therefore, | ||
| + | |||
| + | CMP AL, #97 ; if AL is greater or equal to #97 (' | ||
| + | |||
| + | So therefore if carry is NOT set, skip past the SUB command. Or else (if it's a lowercase ASCII) subtract 32 to convert from lowercase to uppercase. | ||
| + | |||
| + | The second compare has a protective function. | ||
| + | |||
| + | CMP AL, #123 ; if AL >= 123, set carry. | ||
| + | | ||
| + | ; If it's ' | ||
| + | JC @gi_check_keys | ||
| + | |||
| + | |||
| + | So after these two checks, the only values that reach SUB AL, #32 are exactly the bytes from 97 to 122 inclusive; i.e. ' | ||
| + | |||
| + | Visual summary: | ||
| + | ^ AL Value ^ Meaning ^ What Happens ^ | ||
| + | | < 97 | before '' | ||
| + | | 97-122 | '' | ||
| + | | >= 123 | after ' | ||
| + | |||
| + | === Processing player input | ||
| + | We will use the HJKL convention: | ||
| + | |||
| + | <codify armasm> | ||
| + | gi_check_keys: | ||
| + | CMP AL, #' | ||
| + | JZ @move_left | ||
| + | |||
| + | CMP AL, #' | ||
| + | JZ @move_down | ||
| + | |||
| + | CMP AL, #' | ||
| + | JZ @move_up | ||
| + | |||
| + | CMP AL, #' | ||
| + | JZ @move_right | ||
| + | |||
| + | CMP AL, #' | ||
| + | JZ @quit_game | ||
| + | |||
| + | RET | ||
| + | </ | ||
| + | |||
| + | This is easy enough; after a CMP, if the values are equal then the zero flag is set. We will use JZ (jump if zero flag is set) to go to the correct routine. | ||
| + | |||
| + | === processing each command | ||
| + | <codify armasm> | ||
| + | move_left: | ||
| + | LDAL [@PX] ; load variable PX into register AL. | ||
| + | DEC AL ; X = X - 1 (move to the left!) | ||
| + | | ||
| + | CMP AL, #1 ; bounds check. Is the player on the border? | ||
| + | JC @ml_ok | ||
| + | | ||
| + | LDAL #1 ; If we reach here the player was on the border; set position to 1. | ||
| + | | ||
| + | ml_ok: | ||
| + | STAL [@PX] ; pass! Save the player' | ||
| + | RET | ||
| + | </ | ||
| + | |||
| + | This is the move_left command. We begin by loading the player' | ||
sd/part_ii_writing_games_in_assembly_language.1775158821.txt.gz · Last modified: by appledog
