PDA

View Full Version : Need help with rather simple ASSEMBLY program.


Mattpd
02-03-2009, 01:15 AM
Hello. New here, and to programing, and looking for some help with SPARC Assembly. My assignment is to find the maximum value of y = x^6 – 14x^2 +56x, for x=-2,-1,0,1,2,3,and 4. In other words I need to find the maximum y value, and store it in ymax, and store the corresponding x value in xmax. I have already completed and debugged the main part of the program that computes all the values, but I am confused about how to determine the largest values.
I'm guessing that after every run of the loop, you would take the result for y and compare it to what is in ymax. If y is larger than ymax, then it should be replaced by y, and the loop run again. Im just not sure how to do this in assembly though. Also, I have no clue how to find the x value that corresponds with the largest y. I already know that the largest x will produce the largest y, just by looking at the equation. But my goal is to find the x value that corresponds to the largest y value, so just finding the largest x will not do.

This is what I have so far.


/*

cs32114
CSC 3210
Program #2
Due Date Feb 10, 2009
This program will calculate y=(x^6)-(14x^2)+56x for x=-2,-1,0,1,2,3,4
*/

/*Constants are:*/

define(a2,56)
define(a1,14)
define(xmax, l2)
define(ymax, l3)
/*X and Y variable*/
define(x_r,l0)
define(y_r,l1)

.global main
main:
save %sp, -96, %sp
mov -2, %x_r

.global loop
loop:
cmp %x_r, 5
bge done

mov %x_r, %o0 !This will compute x^6, the first part of equation
mov %x_r, %o1
call .mul
nop
mov %x_r, %o1
call .mul
nop
mov %x_r, %o1
call .mul
nop
mov %x_r, %o1
call .mul
nop
mov %x_r, %o1
call .mul
nop
mov %o0, %y_r !completes first part of equation and stores in y.

mov %x_r, %o1 !This will computer 14x^2
mov %x_r, %o0
call .mul
nop
mov a1, %o1
call .mul
nop
sub %y_r, %o0, %y_r !completes first two parts of equation

mov a2, %o0 !computes 56x.
mov %x_r, %o1
call .mul
nop
add %y_r, %o0, %y_r !equation complete. Store result in y_r.

add %x_r, 1, %x_r !x++


ba loop
nop

done:
mov 1, %g1
ta 0

Trinithis
02-03-2009, 07:50 AM
Try writing the code in something like C (or at least in some simple pseudocode) to get a general idea to approach the problem. Also, the largest x value does not always yield the largest y value. For example, you could be given -444 and 10.

(Please note I am unfamiliar with SPARC Assembly. I used MASM, which is for x86 instruction sets.)

Are you allowed to define procedures to use with 'call'? If yes, make the thing that compures the values into its own function and have main do the job of figuring out the largest y value. Is it okay to define a global array containing all the x values? As for the .mul procedure... how does it know the values passed to it? It appears that you are using globals to pass arguments around (ie: x_r, etc). Unless you were instructed to do so, I would highly recommend against it. Instead push the values onto the stack or use registers. Also, remove the nop's. It's a waste of an instruction and makes it appear to have more significance than it really is. If you are just using them as spacing, a simple line comment would suffice in its place (or just a line break).

I thought L0 and family were a memory locations, not a registers. What you were doing is okay.

In style, I would recommend putting a tab (use 8 space width tabs when coding in assembly) between instructions and its operands. Try to line up your comments as well. I also find it handy creating big label comments for sections of the code. If you don't have a text editor with syntax highlighting, now's the time to get one. Clean assembly goes a LONG WAY. For example:

/*
* cs32114
* CSC 3210
* Program #2
* Due Date Feb 10, 2009
* This program will calculate y=(x^6)-(14x^2)+56x for x=-2,-1,0,1,2,3,4
*/

! _________________________________________________________________________
! | |
! | Constants |
! |_________________________________________________________________________|

define(a2, 56)
define(a1, 14)

define(xmax, l2)
define(ymax, l3)

define(x_r, l0) ! X variable
define(y_r, l1) ! Y variable

! _________________________________________________________________________
! | |
! | Data |
! |_________________________________________________________________________|


! _________________________________________________________________________
! | |
! | Code |
! |_________________________________________________________________________|


.global main
main:
save %sp, -96, %sp
mov -2, %x_r

.global loop
loop:
cmp %x_r, 5
bge done

mov %x_r, %o0 !This will compute x^6, the first part of equation
mov %x_r, %o1
call .mul

mov %x_r, %o1
call .mul

mov %x_r, %o1
call .mul

mov %x_r, %o1
call .mul

mov %x_r, %o1
call .mul

mov %o0, %y_r !completes first part of equation and stores in y.

mov %x_r, %o1 !This will computer 14x^2
mov %x_r, %o0
call .mul

mov a1, %o1
call .mul

sub %y_r, %o0, %y_r !completes first two parts of equation

mov a2, %o0 !computes 56x.
mov %x_r, %o1
call .mul

add %y_r, %o0, %y_r !equation complete. Store result in y_r.

add %x_r, 1, %x_r !x++


ba loop

done:
mov 1, %g1
ta 0

oesxyl
02-03-2009, 03:46 PM
if you solve this:

0 = 6*x^5 – 2*14x +56

you will get the x values for minim and maxim, ( 5 solution)

and if you replace what you get in this:

s(x) = 5*6*x^4 – 2*14

you can find what kind of extreme you have based on the sign of s(x), positive or negative.

to write code what compute this:

y = x^6 – 14x^2 +56x


rewrite it as:

y = x*(56 – x*(14 - x^4))


I hope that this, along with Trinithis post, will help you to simplify your work.

best regards

Mattpd
02-03-2009, 07:06 PM
Thank you both. I will go to work cleaning up my code and replacing the nops. Still a tad confused on the actual code writing. I will try to bust something out later today and post any problems.

Trinithis
02-03-2009, 11:46 PM
Here's some pseudocode then:


xs = [-2, -1, 0, 1, 2, 3, 4]

function f(x) {
return x^6 – 14x^2 +56x
}

function main() {
max-x = xs[0]
max-y = f(max-x)
for i = 0 to xs.length {
x = xs[i]
y = f(x)
if y > max-y {
max-x = x
max-y = y
}
}
print max-x
}

Mattpd
02-04-2009, 03:11 AM
It was more simple than I thought and I have completed the code except for one problem. I was instructed to create a label, "done", where the professor will place a break point in GDB and then check to make sure the x_max and y_max values are correct. When I place a break point at done, GDB hits this break point all 6 times, one for each x. By the end, the values are all correct but I should not have to hit "c" to continue 6 times. It should run all the way through, computing all 6 values and then break at the very end, right before it exits. Know what I mean? It sure looks like all my branches are correct, but apparently not.

/*Constants are:*/

define(a2,56)
define(a1,14)

/*X and Y variable*/
define(x_r,l0)
define(y_r,l1)
define(x_max, l2)
define(y_max, l3)

.global main
main:
save %sp, -96, %sp
mov -2, %x_r

.global loop
loop:
cmp %x_r, 5 !Check if X is less than 5
bge done !If not jump to done.

mov %x_r, %o0 !This will compute x^6, the first part of equation
mov %x_r, %o1
call .mul
nop
mov %x_r, %o1
call .mul
nop
mov %x_r, %o1
call .mul
nop
mov %x_r, %o1
call .mul
nop
mov %x_r, %o1
call .mul
nop
mov %o0, %y_r !Completes first part, and stores in y.

mov %x_r, %o1 !This will compute 14x^2
mov %x_r, %o0
call .mul
nop
mov a1, %o1
call .mul
nop
sub %y_r, %o0, %y_r !completes first two parts of equation

mov a2, %o0 !computes 56x.
mov %x_r, %o1
call .mul
nop
add %y_r, %o0, %y_r !equation complete. Store result in y_r.

cmp %y_r, %y_max !Check to see if y_r is larger than previous.
bg update !If so, go to update
cmp %y_r, %y_max
ble loop
nop






update:
mov %y_r, %y_max !y_r was bigger, so it replaces y_max
mov %x_r, %x_max !the corresponding x_r replaces x_max
add %x_r, 1, %x_r !x++
ba loop !Go back to the loop.

done:
mov 1, %g1
ta 0

Zero_Cool
02-09-2009, 07:26 PM
Ok I know it is something simple that I m just not getting

I want to create a template in .asm so that I can add a program to display crap later. CODE below

I want something like this to Display

John Smith
ID: 1111111111111111

m
i
c
r
o
s

now if i take out the John Smith (not my real name) then it displays
the micros like above

but if you run the code it displays
John Smith
ID: 1111111111111111
micros


CODE BELOW



org 0100h

;MY INFO

mov dx,name
mov ah,09h
int 21h
mov ah,4ch
int 21h
name db 'John',0d,'Smith',0ah,'ID: ',' 11111111111',0ah

;PROGRAM STARTS HERE

msg db 'micros','$'
mov DX,msg
mov BX,DX
back:LODSB
mov DL,AL
SUB al, 24h
JZ term
mov AX,0200h
int 21h
mov DL, 0ah
int 21h
mov DL, 08h
int 21h
inc BX
mov DX,BX
JMP back
term:mov AX,4c00h
int 21h