Points of Required Attention™
Please chime in on a proposed restructuring of the ROM hacking sections.
Views: 88,490,311
Main | FAQ | Uploader | IRC chat | Radio | Memberlist | Active users | Latest posts | Calendar | Stats | Online users | Search 04-27-24 12:17 AM
Guest: Register | Login

0 users currently in ROM Hacking | 5 guests | 1 bot

Main - ROM Hacking - Greater-Than or Less-Than ASM? New thread | New reply


RetroRain
Posted on 10-26-13 02:38 AM (rev. 3 of 10-26-13 02:41 AM) Link | Quote | ID: 154946


Fuzz Ball
Level: 66

Posts: 655/994
EXP: 2438301
Next: 23550

Since: 09-30-07

Last post: 1935 days
Last view: 958 days
How do you do greater-than or less-than with 6502? I want to check a range of values. If the value is #$00 - #$11, it will jump to $8FC7, if it is #$12 to #$13, it will jump to $8FCD, and if it is #$14 - #$19, it will jump to $8FD3. Granted, I could simpy do a bunch of CMPs, but I don't want to waste the space. Is there a better way to do it? Thanks.


$8FB9:A5 1B LDA $1B = #$00
$8FBB:C9 00 CMP #$00
$8FBD:F0 08 BEQ $8FC7
$8FBF:C9 12 CMP #$12
$8FC1:F0 0A BEQ $8FCD
$8FC3:C9 14 CMP #$14
$8FC5:F0 0C BEQ $8FD3

$8FC7:A9 26 LDA #$26
$8FC9:8D 06 01 STA $0106 = #$02
$8FCC:60 RTS

$8FCD:A9 27 LDA #$27
$8FCF:8D 06 01 STA $0106 = #$02
$8FD2:60 RTS

$8FD3:A9 28 LDA #$28
$8FD5:8D 06 01 STA $0106 = #$02
$8FD8:60 RTS


____________________
My YouTube Channel

infidelity
Posted on 10-26-13 02:50 AM Link | Quote | ID: 154947


Fuzz Ball
Level: 66

Posts: 408/968
EXP: 2368160
Next: 93691

Since: 05-24-07

Last post: 959 days
Last view: 815 days
First, dont do a cmp of 00, waste of code. If you want to do a cmp of 00, you simply load the register, than follow it with a bne/beq, or whatever branch opcode it is that you want. When comparing multiple values, have your 00 cmp first, then you can follow with c901, f0##, c902, f0##.


never-obsolete
Posted on 10-26-13 02:53 AM Link | Quote | ID: 154948


Rat
Level: 24

Posts: 92/96
EXP: 74499
Next: 3626

Since: 02-22-07
From: Phoenix, AZ

Last post: 2596 days
Last view: 2596 days
-Here's an explanation on how cmp/cpx/cpy work and there's a table showing which branch instructions to use.

http://www.6502.org/tutorials/compare_instructions.html

-An alternate to a long list of compares is a jump table, but that might take up more space.

RetroRain
Posted on 10-26-13 02:53 AM (rev. 2 of 10-26-13 02:58 AM) Link | Quote | ID: 154949


Fuzz Ball
Level: 66

Posts: 656/994
EXP: 2438301
Next: 23550

Since: 09-30-07

Last post: 1935 days
Last view: 958 days
So, are you saying that there is no greater-than or less-than technique? I have no choice but to do a bunch of CMPs?

EDIT - Okay, never-obsolete posted at the same time I made my reply to infidelity's post.

So I can use either BMI, BPL, BCC, or the BCS?

____________________
My YouTube Channel

never-obsolete
Posted on 10-26-13 03:12 AM Link | Quote | ID: 154950


Rat
Level: 24

Posts: 93/96
EXP: 74499
Next: 3626

Since: 02-22-07
From: Phoenix, AZ

Last post: 2596 days
Last view: 2596 days
Yeah.

So to check if the accumulator is less then some value, n:




cmp #n
bcc _isLess
isGreaterEqualTo:
; code if (A >= n)
jmp endblock
isLess:
; code if (A < n)
endblock:


The above is an if-then-else. This can be shortened if you don't need the else by branching on the opposite case:


cmp #n
bcs isGreaterEqualTo
isLess:
; code if (A < n)
isGreaterEqualTo:



There are some tricky ones that require testing for equality first. BCS is one example, it branches when A >= n. So if you wanted to just branch on A > n, you'd first need a BEQ then the BCS. The link I posted covers this stuff.

RetroRain
Posted on 10-26-13 03:17 AM Link | Quote | ID: 154951


Fuzz Ball
Level: 66

Posts: 657/994
EXP: 2438301
Next: 23550

Since: 09-30-07

Last post: 1935 days
Last view: 958 days
Ah, I see. Yeah, sounds a little bit trickier, but I see what you are saying. Alright, I appreciate your help. Thanks for the help both of you.

____________________
My YouTube Channel

Chaobomr
Posted on 10-26-13 06:58 AM Link | Quote | ID: 154952


Buster Beetle
Banned: Spammer takeover?
Level: 45

Posts: 145/467
EXP: 638839
Next: 21325

Since: 05-07-13
From: The dirty south

Last post: 3480 days
Last view: 3479 days
The CMP statement is 6502's version of the infamous
if/then:else
statement?

____________________
Sorry for the stupidity. That jerk will be dealt with in the most insane way possible.

RetroRain
Posted on 10-26-13 08:02 PM (rev. 2 of 10-26-13 08:07 PM) Link | Quote | ID: 154953


Fuzz Ball
Level: 66

Posts: 658/994
EXP: 2438301
Next: 23550

Since: 09-30-07

Last post: 1935 days
Last view: 958 days
Not by itself. The CMP just checks a value. You have to use other opcodes like BCC (Branch on Carry Clear) or BCS (Branch on Carry Set) like never-obsolete gave in the example above. The 6502 has special "status" flags that can be set/un-set, and depending on if the flag is set, you can branch to another spot in the code.

I have used BCC and BCS in my hack MM Nemesis, but I don't personally care for using them though. That's just me. I think because I don't have a lot of experience using them, and I don't like dealing with "flags". I like to simply stay with LDA, STA, CMP, JSR, and JMP opcodes.

Anyway, never-obsolete, after evaluating BCC/BCS, I have decided to use a table instead, because it actually suits my purposes later on down the road when it comes time to do some heavier hacking. It is a lot easier to read, and not all stages require long tables. It saves a lot of checking, and makes it easier to hack later on. But at least I know now how to do the greater-than/less-than stuff. It should come in handy for the future.

Thanks again for the help though.

____________________
My YouTube Channel

Chaobomr
Posted on 10-30-13 01:21 AM Link | Quote | ID: 154986


Buster Beetle
Banned: Spammer takeover?
Level: 45

Posts: 148/467
EXP: 638839
Next: 21325

Since: 05-07-13
From: The dirty south

Last post: 3480 days
Last view: 3479 days
CMP BCC/BCS certainly sounds like those if/then statements that you see in many other languages

____________________
Sorry for the stupidity. That jerk will be dealt with in the most insane way possible.

Main - ROM Hacking - Greater-Than or Less-Than ASM? New thread | New reply

Acmlmboard 2.1+4δ (2023-01-15)
© 2005-2023 Acmlm, blackhole89, Xkeeper et al.

Page rendered in 0.024 seconds. (341KB of memory used)
MySQL - queries: 77, rows: 106/106, time: 0.018 seconds.