Reputation: 13
I'm writing a program in M68K assembly language to find the minimum value in an array.
Here's my code:
ORG $8000
; DATA
array DC.B 2,3,1,4,5 ; The minimum is 1
len EQU 5
min DC.B -128 ; Initialize to lowest possible signed byte
value
START:
lea.l array,a0
move.b (a0)+,d0 ; d0 = 2 (current minimum), a0 now points to 3
moveq.l #len-1,d1 ; One element already used
LOOP:
cmpi.l #0,d1 ; If d1 = 0, no more elements to check
beq.s END ; Jump to the end if finished
move.b (a0)+,d2 ; Load next element into d2
; d2 = 3, a0 -> 1
; d2 = 1, a0 -> 4
; d2 = 4, a0 -> 5
; d2 = 5, a0 -> ??? (What happens here?)
cmp.b d0,d2
blt.s UPDATE ; If d2 < d0, update minimum
bra.s DECREMENT
UPDATE:
move.b d2,d0 ; Update the minimum
DECREMENT:
subq.l #1,d1 ; Decrease counter
bra.s LOOP
END:
move.b d0,min ; Store the minimum
SIMHALT
END START
???
, what happens to a0
after processing the last element 5? Does it point to an invalid address?d0
with move.b (a0)+,d0
a good way to start, or is there a better way?Any suggestions would be greatly appreciated! Thanks!
Upvotes: 1
Views: 37
Reputation: 39506
Beware: your recent edit that sets min = -128
made it worse! You were on the right track before...
- At the point where I marked
???
, what happens toa0
after processing the last element 5? Does it point to an invalid address?
Your min variable follows the array directly, so A0 will point at the first byte of the min longword variable.
- Is my approach of initializing
d0
withmove.b (a0)+,d0
a good way to start, or is there a better way?
That's fine.
- Can my code be optimized further?
Sure!
No need to test for a zero counter at the top of the loop. You could test it before the loop though as a precaution. The decision about iterating some more should be taken below:
subq.l #1, d1 ; Decrease counter
bne.s LOOP
Avoid as much as possible to have to BRA
. If D2 < D0 don't branch to UPDATE, but rather bypass based on the opposite condition which is bge
:
cmp.b d0, d2
bge.s SKIP ; If d2 >= d0, skip updating the minimum
move.b d2, d0 ; Update the minimum
SKIP:
ORG $8000
; DATA
array DC.B 2,3,1,4,5 ; The minimum is 1
len EQU 5
min DC.L 0 ; Stores the found minimum
START:
lea.l array, a0
move.b (a0)+, d0 ; d0 = 2 (current minimum), a0 now points to 3
moveq.l #len-1, d1 ; One element already used
; Could bail-out here if D1 is zero
LOOP:
move.b (a0)+, d2 ; Load next element into d2
cmp.b d0, d2
bge.s SKIP ; If d2 >= d0, skip updating the minimum
move.b d2, d0 ; Update the minimum
SKIP:
subq.l #1, d1 ; Decrease counter
bne.s LOOP
END:
move.b d0, min ; Store the minimum
SIMHALT
END START
You could define min a byte since you're dealing with bytes all the way.
Upvotes: 0