English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 01 November 2012, 19:25   #1
bodhi
Registered User
 
bodhi's Avatar
 
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
Of course, I was expecting the output to be $102c, not $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.
bodhi is offline  
Old 01 November 2012, 19:44   #2
kipper2k
Registered User
 
Join Date: Sep 2006
Location: Thunder Bay, Canada
Posts: 4,323
Quote:
Originally Posted by bodhi View Post
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
Of course, I was expecting the output to be $102c, not $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.

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
kipper2k is offline  
Old 01 November 2012, 22:38   #3
clenched
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.
clenched is offline  
Old 01 November 2012, 23:14   #4
bodhi
Registered User
 
bodhi's Avatar
 
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!
bodhi is offline  
Old 02 November 2012, 08:04   #5
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 7,035
Quote:
Originally Posted by bodhi View Post
so BLT saw bit 16 in $9125 and interpreted it as the number -$1125.
No, it interpreted it as the number -$6EDB. And it is not BLT but CMP which interpretes the numbers. BLT only reacts on the status bits set by CMP.

Try this:

Code:
    clr.l d0
    sub.w #$9125,d0
    PRINTF d0,#FORMAT_HEX
thomas is offline  
Old 02 November 2012, 23:48   #6
bodhi
Registered User
 
bodhi's Avatar
 
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.
bodhi is offline  
Old 25 November 2012, 20:50   #7
mark_k
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).
mark_k is offline  
Old 03 January 2013, 00:55   #8
bodhi
Registered User
 
bodhi's Avatar
 
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
bodhi is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 13:34.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.10269 seconds with 13 queries