Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sparc,asm,loop simple problem - can someone look at my code?

Status
Not open for further replies.

lou99

MIS
Jul 28, 2002
5
0
0
US
Ive been trying to compute a problem.. looks like i have an infinite loop. Im new to Sparc Asm and using the GDB debugger has been a pain. On paper it looks like the program should run, but it doesnt and im completely stuck. This is far fetched but i hope someone can point me in the right direction. all the online resources are way to advanced for me.

what i am trying to compute is....

!y= 3x^4 - 17x^3 + 14x^2 - 23x + 23
!where X<12
!increment counter if y>0

!%o0 temp
!%o1 temp
!%o2 free
!%o3 3x^4
!%o4 17x^3
!%o5 3x^4-17x^3
!%o6 14x^2
!%o7 3x^4-17x^3+14x^2
!%l0
!%l1
!%l2 temporary counter used for x^*
!%l3 x counter
!%l4 23x
!%l5 3x^4-17x^3+14x^2-23x
!%l6 3x^4-17x^3+14x^2-23x-24--> y
!%l7 y (final answer) counter


.global main

main: save %sp, -64, %sp

clr %l3 !initialize counter for x
b test !begin testing
nop

power4:
clr %l2
mov %l3, %o1 !setup to compute x^4
mov %l3, %o0
call .mul
nop
cmp %l2, 4
ble power4 !using loop to compute 3X^4
add %l2, 1, %l2
mov 3, %o1
call .mul
nop
mov %o0, %o3 !storing 3x^4 in %o3

power3:
clr %l2
mov %l3, %o0 !setup to compute x^3
mov %l3, %o1
call .mul
nop
cmp %l2, 3 !using loop to compute
ble power3
add %l2, 1, %l2
mov 17, %o1
call .mul
nop
mov %o0, %o4 !storing 17x^3 to %o4
sub %o3, %o4, %o5 !(3x^4)-(17x^3) storing in %o5

power2:
clr %l2
mov %l3, %o0 !setup x^2
mov %l3, %o1
call .mul
nop
cmp %l2, 2 !using loop to perform x^2
ble power2
add %l2, 1, %l2
mov 14, %o1
call .mul
nop
mov %o0, %o6 !moving 14x^2-->%o6

add %o5, %o6, %o7 !Performing (3x^4)-(17x^3)+(14x^2)

mov 23, %o1 !setup to perform 23x
mov %l3, %o0
call .mul
nop
mov %o0, %l4 !23x

sub %o7, %l4, %l5 !(3x^4)-(17x^3)+(14x^2)-23x

sub %l5, 23, %l6 !final computation (3x^4)-(17x^3)+(14x^2)-23x-23

mov 0, %l7
cmp %l6, 0 !compares y if ans is >0
ble next
add %l7, 1, %l7 !increments counter if y>0

next: add %l3, 1, %l3 !increment counter

test: cmp %l3, 12 ! x<=12 ?
ble power4 ! go back to start

mov 1, %g1
tn 0


----------------------------
I did my best, thx
 

First you can use the horner-rule. Then you need no loops, you save a few multplikations, and gets a more accurat result.

y= 3x^4 - 17x^3 + 14x^2 - 23x + 23
y= (((3*x- 17)*x + 14)*x - 23)*x + 23; ==> only 4 multiplies


anyway the endless-loops in your code is the wrong set af the label power4, power3 and power2.

<b><tt> clr %l2</tt></b>
<b><tt>power4:</tt></b>
mov %l3, %o1 !setup to compute x^4
mov %l3, %o0
call .mul
nop
cmp %l2, 4
ble power4 !using loop to compute 3X^4
add %l2, 1, %l2
mov 3, %o1

the same in the other loops. But try to implement the horner-rule.
 
ahh thank you I see it now, it was clearing each time. I changed the code up.. tried to run it .. it crashed

Program received signal SIGSEGV, Segmentation fault.
0x1068c in power2 ()

(gdb) p $l3
$1 = 33519

that cant be right it should have stopped long ago. Ill keep working at it, thanks for the tip!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top