DEV Community

Qzhang125
Qzhang125

Posted on • Updated on

SPO600 Week 4 String & Char reflection

Hello everyone, welcome back to the 4th week of SPO600(Software Portability and Optimization) reflection and extra exploration blog! In this blog, I'm going to share some thoughts and dig more about the character, string in assembly language.

Characters

First of all, let's see an example!

define      SCREEN    $F000         ;Set the Screen
        LDY #$00            

DRAW_1_CHAR:    LDA STRING, Y           ;Load the String starts from y = 0
        BEQ DONE            ;If the character equal to zero, then jump to done  
        STA SCREEN, Y           ;Load character onto the screen     
        INY             ;Increase Y to read next char
        BNE DRAW_1_CHAR         ;If the STRING is not over, go back to the start
DONE:       BRK 

STRING:
dcb "H", "e", "l", "l", "o", $0D        ;The string to print
Enter fullscreen mode Exit fullscreen mode

In this little program, I am going to read each character from the STRING below then print them on the screen.
First, we need to set up the start location to be $F000.

define      SCREEN    $F000     
Enter fullscreen mode Exit fullscreen mode

Next, We set the register to be 0 for the next loop.
In this loop, all we want to do is read a character from the string and store it into the screen memory. After it, we increase the Y value and run the loop to access the next character in the STRING. when the STRING is empty, we jump out of the loop.
At the end, the result is:
Image description

Now, Let's get one more thing.

define      SCINIT      $ff81 ; initialize/clear screen
define      CHRIN       $ffcf ; input character from keyboard
define      CHROUT      $ffd2 ; output character to screen
define      SCREEN      $ffed ; get screen size
define      PLOT        $fff0 ; get/set cursor coordinates

        JSR SCINIT      ;Reset the screen
PRINT_STRING:   LDY #$00

DRAW_1_CHAR:    LDA STRING, Y
        BEQ DONE
        JSR CHROUT      ;Pass each of the characters to the character out routine
        INY         
        BNE DRAW_1_CHAR 
DONE:       JMP PRINT_STRING    

STRING:
dcb "H", "e", "l", "l", "o", $0D
dcb "W", "o", "r", "l", "d","!",13
dcb 13
Enter fullscreen mode Exit fullscreen mode

In this new program, we add a new loop PRINT_STRINGS to keep printing the string on the screen. An the result is:
Image description

My reflection:
This week we learned how the string and character works in 6502 assembly language. The characters are encoded in binary by numeric reference to a character in a character set. For example, It uses 32 to represent space, 48 represents 0, etc. In 6502 assembly language, string in assembler is stored as sequences of bytes like the example above, it also requires the programmer to deal with the memory as usual.

Top comments (0)