Substraction and Addition

Here you can ask questions or provide insights about how to use efficiently 6502 assembly code on the Oric.
Post Reply
User avatar
peacer
Flight Lieutenant
Posts: 387
Joined: Wed Jun 09, 2010 9:23 pm
Location: Turkey
Contact:

Substraction and Addition

Post by peacer » Tue Dec 25, 2018 11:56 pm

I always find it hard to understand how aritmetic operations are done with machine code. I have a question with 2 byte substraction. Before that, I will try to explain What I do for 1 byte substractions and additions for future references.

Actually, as I previously stated in different topics here, I am not familiar with assembly commands. I am still using old-school hex-codes. LDA command is always #A9 for me. LDA Zero page is #A5 .. I know its really hard but you can't change things after 40 years of life :) . When everything is in HEX, its simple for me. But things get complicated when immediate, zero page, absolute adress definitions .. Whih one use $02 , or #$2 etc.. .. everything gets harder this way to me. Don't blame me. I don't have assembler programs in 80's when I was a kid with limited softwares and a black-red device which changed my life.. I learned machine coding by computer magazine listings of pages of hex numbers :)

Haven't you ever typed-in such things ? I did.. Maybe this is one of the reasons I am using hypermetropic glasses of number 8 today :)
Image

This one is one of treasures hidden in pages of your computer magazine : Oric Lander. Maybe one day I would try to OCR the listing :)
Image

Anyways..

So I will try to "reverse translate" the things I do with Oric explorer and explain it with corresponding BASIC command.

I am trying to build a game for new year competition. I am sure I will not finish it on time so this project will be a future one.

In my machine code routine, movement of a character is done with changing its address at memory location "1" . So, when I change the number in 1 the character will move accordingly. adding 1 = move right, substracting 40 = move up ..

Here is my codes to do this. Don't bother the memory locations. they will be changed.

increment of 1 (Move right) ( DOKE 1, DEEK(1)+1 )

Code: Select all

$042D  18           CLC            
$042E  69 01       ADC #$01        
$0430  85 01       STA $01        
$0432  90 02       BCC $0436     
$0434  E6 02       INC $02       
$0436  60            RTS
decrement of 1 (Move left) ( DOKE 1, DEEK(1)-1 )

Code: Select all

$0455  A5 01       LDA $01        
$0457  D0 02       BNE $045B      
$0459  C6 02       DEC $02        
$045B  C6 01       DEC $01        
$045D  60            RTS
Increment of 40 (Move down) ( DOKE 1, DEEK(1) + 40 )

Code: Select all

$042D  18           CLC            .  
$042E  69 28       ADC #$28
$0430  85 01       STA $01  
$0432  90 02       BCC $0436
$0434  E6 02       INC $02    
$0436  60            RTS
They are all working. But I can't understand how can I decrement 40 from a memory location.

Searching the net, I found this code for 2 byte substraction :
sec
lda num1_low
sbc num2_low
sta result_low
lda num1_high
sbc num2_high
sta result_high
rts
http://retro64.altervista.org/blog/an-i ... -and-more/

I think this code substracts 16 bit NUM2 from NUM1 and put it to HIGH . I don't need 2 byte number for num2. Just 40.

So, can you please help me to do this with my way?

To make it simple, I want a code which will do this basic command : DOKE 1,DEEK(1)-40

Code: Select all

SEC
LDA $01 
SBC #$28
STA $01 
LDA $02 
SBC #$0
STA $02
rts

Does it have to be this way? Does this code work? Why do we have to set carry in the beginning? Any other ideas or simpler solutions?

User avatar
Badger
Officer Cadet
Posts: 36
Joined: Sat Sep 22, 2018 10:04 am
Location: Wigan, England

Re: Substraction and Addition

Post by Badger » Wed Dec 26, 2018 9:50 am

That code for subtraction looks correct to me (not that I am any expert). I dont know if there is any faster way though.

As for setting the carry flag, from whatr I understand it goes like this :-

Consider a simple subtraction of X= 5 - 3

The way the processor handles subtration is actually via an addition so it becomes x=5+(-3) and then minus the complement of the carry bit.
So if the carry bit is 1 then it becomes 0.

So now lets consider it in memory (all numbers are decimal)
$01 = #20
$02 = #10

we want to minus 40 from this.
SEC (set carry bit to 1)
LDA $01 (20)
SBC #40 becomes 20+(-40)-complement of carry (=0)
A now contains 235 and carry bit is set to 0 (it was one when we started due to the SEC)
LDA $02 (10)
SBC #$00 becomes 10+(-0)-complement of carry (1) = 9
STA $02

That is how I understand it. There is are other things with the overflow flag and negative numbers which I've yet to get my head arround but for screen locations I picture it like the above.

I hope it makes some sort of sense.
flag_uk Amateurs built the Ark, Professionals built the Titanic.

User avatar
Chema
Game master
Posts: 2377
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

Re: Substraction and Addition

Post by Chema » Wed Dec 26, 2018 12:46 pm

Subtraction in the 6502 always takes the carry into account, and it works the opposite as with addition. If the carry is clear, the sbc operation additionally subtracts 1.

You need, then, to set it before the subtraction of the low byte and it will be cleared if the result decrement end past zero, so the sbc on the high byte will subtract an additional unit.

Your code is correct, but there is something you can do if you are subtracting a value which is less than FF, and it is the same you do when adding...

Code: Select all

    SEC
    LDA $01 
    SBC #$28
    STA $01 
    BCS SKIP
    DEC $02
SKIP
    RTS
Also you can optimize your +1 a little bit

Code: Select all

    INC $01
    BNE SKIP
    INC $02
SKIP
    RTS
I hope I did not make any mistake... I writing this from memory with my tablet and in a bit of a hurry :)

User avatar
peacer
Flight Lieutenant
Posts: 387
Joined: Wed Jun 09, 2010 9:23 pm
Location: Turkey
Contact:

Re: Substraction and Addition

Post by peacer » Wed Dec 26, 2018 4:49 pm

Thank you very much to both of you :) It sure helps..

Best wishes

User avatar
mikeb
Officer Cadet
Posts: 49
Joined: Wed Sep 05, 2018 8:03 pm
Location: West Midlands, UK
Contact:

Re: Substraction and Addition

Post by mikeb » Sun Jan 27, 2019 8:39 pm

peacer wrote:
Tue Dec 25, 2018 11:56 pm
Maybe one day I would try to OCR the listing :)
A lot of those listings were of questionable readability for human eyes, never mind computer OCR. I think OCR of listings (especially small print) is probably slower than just typing the thing in. By hand. Correcting, using best judgment and skills as an Oric user :)

Of course, this is before you get to the mis-configured printers (was it Oric Owner?) that started emitting fractions instead of "<" and ">" signs, pounds for hashes, inability to tell slashed-0 from 8 from B in dotmatrix font, and all that nonsense.

And the obligatory list~ng that had been printed while Oric still had inter~upts switched on. :(

Post Reply