Substraction and Addition
Posted: 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
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
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 )
decrement of 1 (Move left) ( DOKE 1, DEEK(1)-1 )
Increment of 40 (Move down) ( DOKE 1, DEEK(1) + 40 )
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
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?
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
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
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
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
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
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?