Jump to content







Photo

Action!: Byte Math

Posted by DanBoris, 07 November 2008 · 119 views

Next let’s look at some simple byte variable manipulation. Here is the first sample Action program:

BYTE I

PROC MAIN()
I=1
I=I+2
RETURN

And the assembly code

24B3: .byte $00
24B4: JMP  $24B7	  

24B7: LDY  #$01	 
24B9: STY  $24B3	  
24BC: CLC		
24BD: LDA  $24B3	  
24C0: ADC  #$02	 
24C2: STA  $24B3	  
24C5: RTS

This code starts at memory location $24B3 which is where the global variable I is stored. Next we have a jump instruction that gets us to the MAIN procedure. Like last time this is a little inefficient but not a big problem at the start of the program.

Next we load the immediate value 1 into I. I assume they use the Y register for this so that A can be reserved for math operations which will allow for some optimizations under certain conditions. I have another example that I will post at some point that shows this. In this situation by using Y they actually miss out on the opportunity for an optimization. If A had been used the LDA would not have been necessary. Finally a normal 6502, 8-bit add sequence is executed and the result stored by in I.

Now let’s look at almost the same program, but it will add one to I instead of two:

BYTE I

PROC MAIN()
I=1
I=I+1
RETURN


0E58: .byte #$00
0E59: JMP  $0E5C	  

0E5C: LDY  #$01	 
0E5E: STY  $0E58	  
0E61: INC  $0E58	  
0E64: RTS

As you can see the code is quite a bit smaller. The compiler recognized that I=I+1 is an increment so it uses the INC instruction on the memory location where I is stored resulting in a savings of three instructions.

You may notice that this example is at a different memory location then the first one. I am not actually sure why this happened. I have been compiling these examples using an emulator and those two examples were compiled at different times so it’s possible that I had the emulator configured differently each time.




May 2012

S M T W T F S
  12345
6789101112
13141516171819
202122 23 242526
2728293031  

Recent Entries

Recent Comments

Tags