Help with assembly code


 
Thread Tools Search this Thread
Top Forums Programming Help with assembly code
# 1  
Old 09-24-2010
Help with assembly code

I want make simple assembly code for some thing like this a^6+6a^2+2a and range of a is between -3 to 3. I tried but it is not working properly. As this is my first assembly program that I am going to try, I want some help with it.


I found this example online but i dont want this kind of complicated stuff. I just want to start of with simple programing by using function like call .mul, add, sub thatz all.

Code:
1 ; function quadratic
2 ; finds solutions to the quadratic equation:
3 ; a*x^2 + b*x + c = 0
4 ; C prototype:
5 ; int quadratic( double a, double b, double c,
6 ; double * root1, double *root2 )
7 ; Parameters:
8 ; a, b, c - coefficients of powers of quadratic equation (see above)
9 ; root1 - pointer to double to store first root in
10 ; root2 - pointer to double to store second root in
11 ; Return value:
12 ; returns 1 if real roots found, else 0
13
14 %define a qword [ebp+8]
15 %define b qword [ebp+16]
16 %define c qword [ebp+24]
17 %define root1 dword [ebp+32]
18 %define root2 dword [ebp+36]
19 %define disc qword [ebp-8]
132 CHAPTER 6. FLOATING POINT
20 %define one_over_2a qword [ebp-16]
21
22 segment .data
23 MinusFour dw -4
24
25 segment .text
26 global _quadratic
27 _quadratic:
28 push ebp
29 mov ebp, esp
30 sub esp, 16 ; allocate 2 doubles (disc & one_over_2a)
31 push ebx ; must save original ebx
32
33 fild word [MinusFour]; stack -4
34 fld a ; stack: a, -4
35 fld c ; stack: c, a, -4
36 fmulp st1 ; stack: a*c, -4
37 fmulp st1 ; stack: -4*a*c
38 fld b
39 fld b ; stack: b, b, -4*a*c
40 fmulp st1 ; stack: b*b, -4*a*c
41 faddp st1 ; stack: b*b - 4*a*c
42 ftst ; test with 0
43 fstsw ax
44 sahf
45 jb no_real_solutions ; if disc < 0, no real solutions
46 fsqrt ; stack: sqrt(b*b - 4*a*c)
47 fstp disc ; store and pop stack
48 fld1 ; stack: 1.0
49 fld a ; stack: a, 1.0
50 fscale ; stack: a * 2^(1.0) = 2*a, 1
51 fdivp st1 ; stack: 1/(2*a)
52 fst one_over_2a ; stack: 1/(2*a)
53 fld b ; stack: b, 1/(2*a)
54 fld disc ; stack: disc, b, 1/(2*a)
55 fsubrp st1 ; stack: disc - b, 1/(2*a)
56 fmulp st1 ; stack: (-b + disc)/(2*a)
57 mov ebx, root1
58 fstp qword [ebx] ; store in *root1
59 fld b ; stack: b
60 fld disc ; stack: disc, b
61 fchs ; stack: -disc, b
6.3. THE NUMERIC COPROCESSOR 133
62 fsubrp st1 ; stack: -disc - b
63 fmul one_over_2a ; stack: (-b - disc)/(2*a)
64 mov ebx, root2
65 fstp qword [ebx] ; store in *root2
66 mov eax, 1 ; return value is 1
67 jmp short quit
68
69 no_real_solutions:
70 mov eax, 0 ; return value is 0
71
72 quit:
73 pop ebx
74 mov esp, ebp
75 pop ebp
76 ret

# 2  
Old 09-24-2010
Try writing the code in C/C++ and compiling to an assembly file instead of a binary object.

Code:
gcc -O3 -S code.c

See how GCC does what you want.

You can change the optimization levels and see what effect they have.
# 3  
Old 09-24-2010
The assembly code example you got from the web is for x86 not sparc that is if you are on a Solaris platform using a sparc chipset...besides you mentioned that in an earlier thread. Search the web for sparc assembly language tutorials because thats what is relevant to your needs.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Understanding Assembly Code

As the title suggests, I want to better understand the following assembly code: section .text global main ; must be declared for linker (gcc) main: ; tell linker entry point mov edx, len ; message length mov ecx, msg ; message to write... (2 Replies)
Discussion started by: Azrael
2 Replies

2. Shell Programming and Scripting

auto-generating assembly code by variables found by script

Hi everybody I'm working on a list of registers(flip-flops to be exact), now i need to extract some value from this list and use them as arguments to pass them to some assembly code for example i have: 118 chain79 MASTER (FF-LE) FFFF 1975829 /TCK F FD1TQHVTT1 ... (1 Reply)
Discussion started by: Behrouzx77
1 Replies

3. Homework & Coursework Questions

Sparc Assembly

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am having a hard time with this assignement i cant get the pfib value to print out the fib sequence. ... (1 Reply)
Discussion started by: kenjiro310
1 Replies

4. Programming

Assembly 8085

Hi eneryone, Im trying to learn about assemply 8085 in order to make a project i have. First of all which emulator to use? I work in linux and I currently istalled GNUSim8085. Also, maybe somewhere I can find some simple examples? The project is about making an array saved in specific... (8 Replies)
Discussion started by: giampoul
8 Replies

5. Programming

Why Assembly Language?

Hi guys, Assembly language is a low level language designed in 1950's, both system and application programs were written in assembly at that time. There is no question of assembly being very effecient compared to the other high level languages. But it is very cumbersome to write programs in... (9 Replies)
Discussion started by: gabam
9 Replies

6. Programming

Fibonacci (assembly)

When i run this with gcc filename.s -o filename. It is giving fatal error: Unknown opcode at define registers. Anyhelp will be appreciated .section ".data" prompt: .asciz "\nThis program prints the Fibonacci sequence" prompt2: .asciz "\nEnter a limit on the largest number to be displayed:"... (2 Replies)
Discussion started by: Learnerabc
2 Replies

7. Programming

Assembly Language ( compile )

Can someone explain how to compile assembly language code. I made my first assembly language code, but don't know how to run it. I know i can debug with gdb, but is there any way to just compile it and get the answer like in C and java compilers. (3 Replies)
Discussion started by: Learnerabc
3 Replies

8. Programming

Need assembly code for C program

Dear Buddies, I need assembly code for a compiled c program in unix. Kindly help me.... Thanking you in advance. (1 Reply)
Discussion started by: karthik537
1 Replies

9. Shell Programming and Scripting

Unix assembly help

Hey guys, well I'm extremely knew to Unix so any help advice is very appreciated. What I am trying to do is assemble a source code (Programming from the Ground Up: Chapter 3: Your First Programs) The source code named "FINDING A MAXIMUM VALUE" is the code I am trying to link and run, but I... (1 Reply)
Discussion started by: ac09
1 Replies

10. UNIX for Dummies Questions & Answers

Assembly vs C programming

I am currently looking into an os on developed on an x86 platformwhich contains certain assembly code written in .s files(NASM- compatible ) instead of using C I was wondering why C could not be used to do low level stuff instead? Do anyone have any idea or could enligthen me?Appreciate any help... (0 Replies)
Discussion started by: duoshock
0 Replies
Login or Register to Ask a Question