DEV Community

Qzhang125
Qzhang125

Posted on • Updated on

Week3 SPO600 Math Problem Solving

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 use 6502 assembly language to do some math problems.

In 6502 assembly language, we use ADC(add with carry) and SBC(subtract with carry) to add and subtract the value which is stored in the accumulator.

With the basic ideology, let's create a simple program to calculate numbers.

LDY #$05    ;Load Y register with 40 in hexadecimal

SEC     ;Set carry flag
TYA         ;Transfer Y register to the accumulator
SBC #$02        ;Do the subtraction 2 
CLC             ;Clear carry flag
ADC #$01
TAY     ;Transfer the A register to Y
Enter fullscreen mode Exit fullscreen mode

In this simple program, we load a number 5 to the Y register in hex decimal. For subtraction, we have to use the SEC to set up a carry flag. Next, we transfer the value which is stored in Y to the accumulator for processing calculation. Afterwards, we subtract carry 2 to the number in the accumulator, then CLC is used to clear the flag for the addition with carry 1. Lastly we transfer the result from the accumulator to the Y register. In this little program we simply do a 5-2+1 calculation by using assembly language. As we expected, the answer is 4 in the Y register!
Image description

My thoughts about the math problem in the assembly world. Personally, I start the programming with a high level programming language, creating a program to do the calculation above is the easiest thing for me. But in assembly language, numbers are stored in registers. If we want to access them and modify the number we have to set them into the accumulator, then create or clear carry flags for addition or subtraction. It shows the property of a low-level programming language which has to actually access the memory by the programmer every single time during the programming.

Top comments (0)