Lab 2: Assembly Language Lab
In the first week of the course, I was introduced with
Assembly Language and got to know its importance in
software optimization. The 6502 Emulator was used as a learning environment for
experimenting with Assembly Code. In lab 2, we had to form a group and learned to get used to some of the basic opcodes of Assembly Language. The content of
this post is a summary of what I’ve learned so far from the lab.
The block of code below was provided as a starting
point for this lab.
Running this block of code will fill the top right
screen with Yellow. In order to understand what is going on here, there’re a
few things that we need to know first. The screen’s color is originally black
and it is divided into four smaller rectangle blocks (which can also be called
pages) defined by pixels. Each rectangle block has exactly 256 pixels. The
first four lines of code are used to store the value of the first page, $0200, at
address $40. And then, we load the value of the Yellow color into the
Accumulator and set a counter at register Y which is used to keep track of the
current position of the pixel on the screen. We begin the work of filling the
screen with Yellow at a block of code named “loop”. We use STA opcode, which
means “store accumulator to memory”, to make a pixel turn Yellow. The “y” after
the comma indicates the position of the pixel that is to become Yellow. The
value stored in register Y will be incremented in order to turn pixel after
pixel into Yellow until a page is filled. The last four lines of code is used to
increment the page’s value after a page is filled. The code finishes running when
the page’s value reaches $0600 which doesn’t exist because there’s no fifth
page.
This is the very basic of Assembly Code’s syntax and
opcode that we have to understand first. We got to learn more new opcodes and
concepts and experiment with them further down the lab.
è TYA
means “transfer the value from register Y to the Accumulator”. When we add TYA
before the STA, the accumulator will load the value register y currently has so
that the value can be used for the next line of code. That value will be
interpreted as a color when the code steps to STA for displaying on the screen.
Whenever the value at register Y is incremented, the value at the Accumulator
will change, thus changing the color. However, the 6502 Emulator can only
display a maximum of 16 different colors. Therefore, after all 16 colors have
been displayed, the color value will return back to value 1 and continue to
increase until it reaches 16. The cycle continues like this until all the pages
are filled. This is the reason why 16 different colors are displayed twice on
the same line
è LSR
means “Logical Shift Right” which will inform the processor to shift all the
bits on the previous line of code to the right by 1 bit. Therefore, when I
added LSR after TYA, the value stored in the Accumulator will have its bits
shifted right, which means the value is divided by two. However, there is no
way to represent decimal numbers in binary, so the number to the right of the
decimal point is discarded. Consequently, whenever the value in the Accumulator
is increased to an odd number, it will be shifted back to the previous value that
is already presented on the screen. For example, the value 0 will be displayed
on the screen first and then it is increased to 1 in the next loop. Before the
value is displayed, it will be shifted right, which means it will be dived by
2. However, 1 divided by 2 is 0.5 which cannot be represented in binary, so 0
will be its actual value. This is why we have another 0 after the first 0 is
displayed. The pattern continues like this with 16 values of color.
Consequently, the colors displayed on the screen get one pixel thicker.
What would happen if more
LSR opcodes were added?
è In
this case, the value has its bits shifted will be divided by 2n
where n is the number of LSR in the code. Consequently, if one LSR makes a
color one pixel thicker, then n LSR will make a color n pixel thicker. Moreover,
since a horizontal line on the bitmap screen can only store 32 bits, the color
value will be shown on the next line if one line is not enough for all 16
colors.
è ASL
means “Arithmetic Shift Left” which does the opposite to LSR, shifting all bits
to the left. In this case, instead of being divided by 2n, the value
will be multiplied by 2n, thus skipping some of the colors. Consequently,
the color cycle will be smaller, instead of the normal cycle with 16 colors
4. What
would happen if I went to back the original code and added more INY after STA?
è When
I was experimenting with this scenario, I found the way the screen was filled
interesting. Basically, when I added an even number of INY, the color was
spaced out evenly depending on the number of INY added. For example, if two or
four INY is added, then a colored pixel will appear in every two or four blank
pixels respectively. However, when I added an odd number of INY, the screen was
completely filled with color, leaving no blank pixel regardless of the odd
number. I figured out how this worked when the professor explained it to the
class. When an odd number of INY are used, each colored pixel will be spaced
out unevenly, which makes it impossible to reach the ending bit of a page.
Consequently, the code needs to be looped again until the ending bit is
reached, thus filling the whole page with color. This is not the case for an
even number of INY since the colored pixel is spaced out equally, which makes
it easier to reach the ending bit.
Let’s move onto the interesting
part of this lab: Writing Code:
1. Draw
a green line across the top of the bitmap screen and a blue line across the
bottom.
Drawing a line across the
top and bottom of the screen is relatively straightforward. In this scenario, I
need to know the value of the first bit of the first line and the last line. The
first bit of the first line has a value of $0200, while the first bit of the last
line has a value of $05e0. Both values serve as starting points for coloring across
the top and bottom lines. Each line has exactly 32 bits, so I need to stop coloring
the pixels if I hit the end of a line. This is the reason why I compare the
current bit value with $20 which is a value equivalent to 32 bits. To sum up,
knowing the correct values will help a lot in finishing this task.
2. Extend
the previous code to draw a yellow line down the left side of the screen and a
purple line down the right side.
This is a little bit
trickier, because I need to draw a vertical line instead of a horizontal line.
I have to use “ADC $1f” to jump to next line after a pixel is colored. ADC
means “Add Carry”. The value next to ADC opcode will be added to the value in
the previous line of code. In this case, I add $1f, which is equivalent to 31
bits, to the value in the Accumulator so that the next bit which needs to be
colored will start on the next line.
In conclusion, this lab has taught me a lot about
Assembly language and it got me to get used to it in a fun way. With the
knowledge acquired from this lab, I will be more prepared for a more advanced concepts
introduced in the future
Comments
Post a Comment