* suffix or operands invalid for
@ 2006-12-22 2:39 Kenton Brede
2006-12-22 12:51 ` Frank Kotler
0 siblings, 1 reply; 8+ messages in thread
From: Kenton Brede @ 2006-12-22 2:39 UTC (permalink / raw)
To: linux-assembly
I've done some scripting but don't consider myself a programmer. I
ran across "Programming from the Ground Up" and thought it would be an
interesting project. At any rate I'm having problems compiling a
program I've copied out of the book. I'm hoping someone can help me
out. Thanks for any help.
Kent
Here are the errors when I run "as power.s -o power.o":
power.s: Assembler messages:
power.s:14: Error: suffix or operands invalid for `push'
power.s:15: Error: suffix or operands invalid for `push'
power.s:19: Error: suffix or operands invalid for `push'
power.s:22: Error: suffix or operands invalid for `push'
power.s:23: Error: suffix or operands invalid for `push'
power.s:27: Error: suffix or operands invalid for `pop'
power.s:61: Error: suffix or operands invalid for `push'
power.s:85: Error: suffix or operands invalid for `pop'
The code:
------------------------------------------------------------------------------------------------------------
.section .data
.section .text
.globl _start
_start:
pushl $3 #push second argument
pushl $2 #push first argument
call power #call the function
addl $8, %esp #move the stack pointer back
pushl %eax #save the first answer before
#calling the next function
pushl $2 #push second argument
pushl $5 #push first argument
call power #call the function
addl $8, %esp #move the stack pointer back
popl %ebx #The second answer is already
#in %eax. We saved the
#first answer onto the stack,
#so now we can just pop it
#out into %ebx
addl %eax, %ebx #add them together
#the result is in %ebx
movl $1, %eax #exit (%ebx is returned)
int $0x80
.type power, @function
power:
pushl %ebp #save old base pointer
movl %esp, %ebp #make stack pointer the base pointer
subl $4, %esp #get room for our local storage
movl 8(%ebp), %ebx #put first argument in %eax
movl 12(%ebp), %ecx #put second argument in %ecx
movl %ebx, -4(%ebp) #store current result
power_loop_start:
cmpl $1, %ecx #if the power is 1, we are done
je end_power
movl -4(%ebp), %eax #move the current result into %eax
imull %ebx, %eax #multiply the current result by
#the base number
movl %eax, -4(%ebp) #store the current result
decl %ecx #decrease the power
jmp power_loop_start #run for the next power
end_power:
movl -4(%ebp), %eax #return value goes in %eax
movl %ebp, %esp #restore the stack pointer
popl %ebp #restore the base pointer
ret
----------------------------------------------------------------------------------------------------------------------------
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: suffix or operands invalid for
2006-12-22 2:39 suffix or operands invalid for Kenton Brede
@ 2006-12-22 12:51 ` Frank Kotler
2006-12-22 14:18 ` Kenton Brede
0 siblings, 1 reply; 8+ messages in thread
From: Frank Kotler @ 2006-12-22 12:51 UTC (permalink / raw)
To: Kenton Brede; +Cc: linux-assembly
Kenton Brede wrote:
> I've done some scripting but don't consider myself a programmer.
Ah. "Denial". Don't worry, it isn't so bad when you get used to it. :)
> Here are the errors when I run "as power.s -o power.o":
>
> power.s: Assembler messages:
> power.s:14: Error: suffix or operands invalid for `push'
... etc.
Your code... or Jonathan's... assembles silently and sweetly for me.
"as --version" reports:
GNU assembler 2.15.90.0.3 20040415
Copyright 2002 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License. This program has absolutely no warranty.
This assembler was configured for a target of `i486-slackware-linux'.
What's yours?
Only thing I can suggest trying is dump the "l" from "pushl"/"popl" -
but that should be fine! I'm stumped.
Best,
Frank
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: suffix or operands invalid for
2006-12-22 12:51 ` Frank Kotler
@ 2006-12-22 14:18 ` Kenton Brede
2006-12-22 16:16 ` Robert Plantz
2006-12-31 18:06 ` Hendrik Visage
0 siblings, 2 replies; 8+ messages in thread
From: Kenton Brede @ 2006-12-22 14:18 UTC (permalink / raw)
To: linux-assembly
On 12/22/06, Frank Kotler <fbkotler@verizon.net> wrote:
> Kenton Brede wrote:
> > I've done some scripting but don't consider myself a programmer.
>
> Ah. "Denial". Don't worry, it isn't so bad when you get used to it. :)
:)
> > Here are the errors when I run "as power.s -o power.o":
> >
> > power.s: Assembler messages:
> > power.s:14: Error: suffix or operands invalid for `push'
> ... etc.
>
> Your code... or Jonathan's... assembles silently and sweetly for me.
>
> "as --version" reports:
>
> GNU assembler 2.15.90.0.3 20040415
> Copyright 2002 Free Software Foundation, Inc.
> This program is free software; you may redistribute it under the terms of
> the GNU General Public License. This program has absolutely no warranty.
> This assembler was configured for a target of `i486-slackware-linux'.
>
> What's yours?
$ as --version
GNU assembler 2.16.91 20060118 Debian GNU/Linux
Copyright 2005 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License. This program has absolutely no warranty.
This assembler was configured for a target of `x86_64-linux-gnu'.
<snip>
Thanks to both of you for your replies. You've helped me identify the
problem. I'm trying to assemble the code on an x86_64 system. I
tried the code using two identical versions of as, except one was 64
bit and the other 32. The assembly code assembled fine on the 32 bit.
So I don't know if the code simply doesn't work with the 64 bit CPU or
the language of the program doesn't work with the 64 bit assembler.
At any rate I tried:
$ as --32 power.s -o power.o
$ ld power.o -o power
and got
ld: warning: i386 architecture of input file `power.o' is incompatible
with i386:x86-64 output
I then found if I put ".code32" at the top of the code it compiled but
seg faulted.
I'll use the 32 bit machine from now on.
Thanks,
Kent
--
"It may be true that the law cannot make a man love me, but it can stop him
from lynching me, and I think that's pretty important." - Martin
Luther King Jr.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: suffix or operands invalid for
2006-12-22 14:18 ` Kenton Brede
@ 2006-12-22 16:16 ` Robert Plantz
2006-12-22 16:55 ` Robert Plantz
2006-12-31 18:06 ` Hendrik Visage
1 sibling, 1 reply; 8+ messages in thread
From: Robert Plantz @ 2006-12-22 16:16 UTC (permalink / raw)
To: Kenton Brede; +Cc: linux-assembly
On Fri, 2006-12-22 at 08:18 -0600, Kenton Brede wrote:
> On 12/22/06, Frank Kotler <fbkotler@verizon.net> wrote:
>
> At any rate I tried:
>
> $ as --32 power.s -o power.o
>
> $ ld power.o -o power
>
First, at this point, if you do
gcc -m32 power.o -o power
gcc will go directly to the ld phase (since it can tell that power.o is
already compiled/assembled). But the big advantage is that gcc will
automatically link in any C libraries needed to run your code. That is,
it links in all the set up stuff so you can start your programs with a
"main" function.
Then "main" functions will need to do:
.globl main
main: pushl %ebp # save caller's base pointer
movl %esp, %ebp # establish our base pointer
# add to %esp for local vars here
# save registers used in this function
# now your code goes here
# restore registers
movl $0, %eax # return 0; for our shell
movl %ebp, %esp # get rid of local var space on stack
popl %ebp # for our caller
ret
You can see how gcc does this by writing a main function in C, then
doing
gcc -m32 -S foo.c
The "-S" (yes, that's upper case S) produces foo.s, which is gcc's
assembly language version of foo.c. You will lots of messing around with
the stack pointer beyond what I've shown above. I think that's to keep
the stack pointer on "nice" addressing boundaries. But what I've shown
above gets the job done while learning this stuff.
Next, I don't know what distro you're running. I run both Ubuntu Edgy
and Debian Etch. On these distros I needed to install the libc6-dev-i386
package. I also have ia32-libs and libc6-i386 installed.
Before I installed libc6-dev-i386, the error message I got when I
invoked the link stage (using gcc as above) was
bob@debian:~$ gcc -m32 yes_no2.o -o yes_noxx
/usr/bin/ld: skipping incompatible /usr/bin/../lib/libc.so when
searching for -lc
/usr/bin/ld: skipping incompatible /usr/bin/../lib/libc.a when
searching for -lc/usr/bin/ld: skipping incompatible /usr/lib/libc.so
when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib/libc.a when searching for
-lc
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
bob@debian:~$ gcc -m32 yes_no2.o -o yes_noxx
(Sorry for the word wrapping here; hope you can read it.)
On earlier Unbuntu releases, I think the required things were in
different packages, but I'm not sure.
Bob
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: suffix or operands invalid for
2006-12-22 16:16 ` Robert Plantz
@ 2006-12-22 16:55 ` Robert Plantz
2006-12-22 19:05 ` Kenton Brede
0 siblings, 1 reply; 8+ messages in thread
From: Robert Plantz @ 2006-12-22 16:55 UTC (permalink / raw)
To: Kenton Brede; +Cc: linux-assembly
On Fri, 2006-12-22 at 08:16 -0800, Robert Plantz wrote:
> On Fri, 2006-12-22 at 08:18 -0600, Kenton Brede wrote:
> > On 12/22/06, Frank Kotler <fbkotler@verizon.net> wrote:
> >
> > At any rate I tried:
> >
> > $ as --32 power.s -o power.o
> >
> > $ ld power.o -o power
> >
>
> First, at this point, if you do
> gcc -m32 power.o -o power
Oops. If it wasn't clear, I meant to use gcc instead of ld for the
linking phase. I.e.,
as --32 --gstabs power.s -o power.o
gcc -m32 power.o -o power
The "--gstabs" option will include debugging info. Then you can use gdb
to single step through your program etc. Great learning tool.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: suffix or operands invalid for
2006-12-22 16:55 ` Robert Plantz
@ 2006-12-22 19:05 ` Kenton Brede
2006-12-22 19:15 ` Robert Plantz
0 siblings, 1 reply; 8+ messages in thread
From: Kenton Brede @ 2006-12-22 19:05 UTC (permalink / raw)
To: linux-assembly
On 12/22/06, Robert Plantz <plantz@sonoma.edu> wrote:
> On Fri, 2006-12-22 at 08:16 -0800, Robert Plantz wrote:
> > On Fri, 2006-12-22 at 08:18 -0600, Kenton Brede wrote:
> > > On 12/22/06, Frank Kotler <fbkotler@verizon.net> wrote:
> > >
> > > At any rate I tried:
> > >
> > > $ as --32 power.s -o power.o
> > >
> > > $ ld power.o -o power
> > >
> >
> > First, at this point, if you do
> > gcc -m32 power.o -o power
>
> Oops. If it wasn't clear, I meant to use gcc instead of ld for the
> linking phase. I.e.,
>
> as --32 --gstabs power.s -o power.o
> gcc -m32 power.o -o power
>
> The "--gstabs" option will include debugging info. Then you can use gdb
> to single step through your program etc. Great learning tool.
I tried that and the output of gcc gave me enough information that I
was able to get the prog to compile on the system.
Thanks :)
Kent
--
"It may be true that the law cannot make a man love me, but it can stop him
from lynching me, and I think that's pretty important." - Martin
Luther King Jr.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: suffix or operands invalid for
2006-12-22 19:05 ` Kenton Brede
@ 2006-12-22 19:15 ` Robert Plantz
0 siblings, 0 replies; 8+ messages in thread
From: Robert Plantz @ 2006-12-22 19:15 UTC (permalink / raw)
To: Kenton Brede; +Cc: linux-assembly
On Fri, 2006-12-22 at 13:05 -0600, Kenton Brede wrote:
> I tried that and the output of gcc gave me enough information that I
> was able to get the prog to compile on the system.
Another program you may find useful is
objdump -d myProg
where "myProg" is the name of your executable. This will give you a
disassembled view of your program as it looks in memory. You can see
where the external library functions are called, etc. It's a long
display, so pipe it through more or less.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: suffix or operands invalid for
2006-12-22 14:18 ` Kenton Brede
2006-12-22 16:16 ` Robert Plantz
@ 2006-12-31 18:06 ` Hendrik Visage
1 sibling, 0 replies; 8+ messages in thread
From: Hendrik Visage @ 2006-12-31 18:06 UTC (permalink / raw)
To: Kenton Brede; +Cc: linux-assembly
On 12/22/06, Kenton Brede <kbrede@gmail.com> wrote:
> Thanks to both of you for your replies. You've helped me identify the
> problem. I'm trying to assemble the code on an x86_64 system. I
> tried the code using two identical versions of as, except one was 64
> bit and the other 32. The assembly code assembled fine on the 32 bit.
>
> So I don't know if the code simply doesn't work with the 64 bit CPU or
> the language of the program doesn't work with the 64 bit assembler.
The longmode/64bit mode of the AMD64s doesn't do pushing of
32bits only 8 and 64bits AFAIR. I'll have to investigate myself why/what
the technical reasoning behind that is/was but it's currently for me
JustOneOfThoseThingsInLife(TM) ;^)
--
Hendrik Visage
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2006-12-31 18:06 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-12-22 2:39 suffix or operands invalid for Kenton Brede
2006-12-22 12:51 ` Frank Kotler
2006-12-22 14:18 ` Kenton Brede
2006-12-22 16:16 ` Robert Plantz
2006-12-22 16:55 ` Robert Plantz
2006-12-22 19:05 ` Kenton Brede
2006-12-22 19:15 ` Robert Plantz
2006-12-31 18:06 ` Hendrik Visage
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).