![]() |
![]() |
#1 |
Registered User
Join Date: Nov 2012
Location: GB
Posts: 32
|
Noobie Help (conditional branching)
Hey guys,
I'm currently teaching myself 68k assembly using an excellent book called 68000 Assembly Language Programming (2e by Hawkins, Cramer, etc). Things have been going pretty well so far but I'm now stuck on one of the problems from the book. The problem is very basic: find the smallest of three numbers stored in memory. I thought the following code would do the trick: Code:
SmallestNumber clr.l d0 lea input2,a0 move.w (a0),d0 cmp.w 2(a0),d0 blt AAA move.w 2(a0),d0 AAA cmp.w 4(a0),d0 blt BBB move.w 4(a0),d0 BBB PRINTF d0,#FORMAT_HEX rts input2 dc.w $9125,$102c,$7040 output2 ds.w 1 cnop 0,2 Code:
OUTPUT: $9125 ![]() Hopefully it's clear what I'm trying to make the code do but can anyone here point out what I'm doing wrong... ?? Environment: Cross compiling using VBCC/VASM under Windows. WinUAE. |
![]() |
![]() |
#2 | |
Registered User
Join Date: Sep 2006
Location: Thunder Bay, Canada
Posts: 4,323
|
Quote:
Hi, Been a while since i done some useful coding but i think that LEA input2,a0 means to write the contents of long word (and not word) into A0. could be wrong, use a tracer and trace each step that will show you whats wrong. You can even enter it into your amiga using action replay and tracestep it too, its only a short program |
|
![]() |
![]() |
#3 |
Registered User
Join Date: Sep 2008
Location: Gainesville U.S.A.
Posts: 771
|
BLT is for signed numbers so $9125 is negative and the smallest number.
Change BLT to BCS to get the expected result. |
![]() |
![]() |
#4 |
Registered User
Join Date: Nov 2012
Location: GB
Posts: 32
|
clenched: aha! right, so BLT saw bit 16 in $9125 and interpreted it as the number -$1125. Ok, makes sense now my memories of binary number representation come back.
The book I'm reading is quite cruel. Hasn't covered anything on dealing with signed numbers yet, and gives no solutions or hints to problems. (still a very good book though) Anyway, thanbk you both for the help! |
![]() |
![]() |
#5 | |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 7,035
|
Quote:
Try this: Code:
clr.l d0 sub.w #$9125,d0 PRINTF d0,#FORMAT_HEX |
|
![]() |
![]() |
#6 |
Registered User
Join Date: Nov 2012
Location: GB
Posts: 32
|
ok, so $9125 is (NOT $6EDB)+$0001 (two's compliment) of $6EDB. I think I have that bit understood now.
I'm only at page 70 of 500 in the assembly book I'm reading, so it hasn't gone into much detail on how the commands work. But what you say about CMP and Bcc made me read their descriptions to figure out how my bit of code is actually working: - CMP works by subtracting source op from destination. - If that comparison requires a carry then the source is larger than the destination and CMP will then set the C status flag. - BCS looks at this C flag and branches if it is set. I think things are becoming clearer. thank you Last edited by bodhi; 02 November 2012 at 23:53. |
![]() |
![]() |
#7 |
Registered User
Join Date: Aug 2004
Location:
Posts: 3,351
|
Some assemblers accept alternative mnemonics for BCC and BCS which can make writing code with unsigned comparisons easier; BCS = BLO (branch if lower) and BCC = BHS (branch if higher or same).
|
![]() |
![]() |
#8 |
Registered User
Join Date: Nov 2012
Location: GB
Posts: 32
|
mark_k: but I'm only forcing myself to struggle with ASM for a better understanding of how computers work. BCC is better (for me), because I have to think more about how the CPU processes information. BHS/BLO are too human
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
noobie | Mr.pip | support.Hardware | 8 | 19 February 2009 04:01 |
Sorry, noobie question | Antoshi | support.WinUAE | 1 | 04 March 2006 12:28 |
|
|