LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2010, 10:48 AM   #46
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197

Quote:
Originally Posted by MTK358 View Post
so if you don't need any parameters, return values, or local vars, you can just use call and return?
If you also don't modify any of the callee saved registers specified by the ABI, yes.

Quote:
are all parameters and local vars accessed as offsets from %ebp in the main body of the function?
Usually, but if you prefer you can access them as offsets from esp instead.

If you follow the convention, ebp will point to the same spot in the stack frame through the entire body of the function.

esp typically changes during the execution of the function, so the offset from esp needed to access a parameter or local variable will be different at some place within the body of the function from some other place in the body of the same function. That can quckly lead to coding errors.

But if you find yourself short of general purpose registers (pretty common in 32 bit x86) you may find it worth the trouble to use ebp to hold some other pointer, while you access parameters and local variables using esp.
 
Old 03-05-2010, 11:36 AM   #47
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I created this program, it's return value should be 2 to the 3rd power:

Code:
.section .data

.section .text

.globl _start

# Main

_start:

pushl $3
pushl $2
call power
addl $8, %esp

movl %eax, %ebx
movl $1, %eax
int $0x80

##### Function power #####
# Parameters:
# 
# base: 8(%ebp)
# power: 12(%ebp)
# 
# Local Vars:
# 
# result: -4(%ebp)

.type power, @function
power:
	pushl %ebp
	movl %esp, %ebp
	subl $4, %esp
	
	power_loop_start:
		imull -4(%ebp), 8(%ebp)
		decl 12(%ebp)
		cmpl 0, 12(%ebp)
		jle power_loop_end
	jmp power_loop_start
	power_loop_end:
	
	movl -4(%ebp), %eax
	
	movl %ebp, %esp
	popl %ebp
ret
But the assembler gives these warnings:

Code:
$ as asmtut.s -o asmtut.o
asmtut.s: Assembler messages:
asmtut.s:11: Error: suffix or operands invalid for `push'
asmtut.s:12: Error: suffix or operands invalid for `push'
asmtut.s:32: Error: suffix or operands invalid for `push'
asmtut.s:37: Error: too many memory references for `imul'
asmtut.s:39: Error: too many memory references for `cmp'
asmtut.s:47: Error: suffix or operands invalid for `pop'
What do you think is wrong?
 
Old 03-05-2010, 12:03 PM   #48
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
I dont use GNU as (I use nasm and tasm) but WTH are these:
Code:
8(%ebp)
Oh and those assembler messages are errors and not warnings.
----edit----
Shouldn't it be more like:
Code:
mov 2,eax
mov 3,ebx
LOOP_BEGIN:
imul 2,eax
dec ebx
test ebx,ebx
jnz LOOP_BEGIN
(Kind of a mish-mash between intel and at&t syntax)

Last edited by smeezekitty; 03-05-2010 at 12:07 PM.
 
Old 03-05-2010, 12:09 PM   #49
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by smeezekitty View Post
I dont use GNU as (I use nasm and tasm) but WTH are these:
Code:
8(%ebp)
AFAIK that means get the number at the memory address stored in %ebp offset by 8 bytes.

Quote:
Originally Posted by smeezekitty View Post
Oh and those assembler messages are errors and not warnings.
I know.

Quote:
Originally Posted by smeezekitty View Post
Code:
mov 2,eax
mov 3,ebx
What are 2 and 3?

Last edited by MTK358; 03-05-2010 at 12:12 PM.
 
Old 03-05-2010, 12:41 PM   #50
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MTK358 View Post
What do you think is wrong?
1) Not every operand type (constant, memory reference, register) is valid for every operand of every instruction.

Most instructions support just a few combinations of operands and many operands can only be registers, in some cases only specific registers.

You should be working with a reference manual for the instruction set, so you have that info available.

2) You also have a logic error (which the assembler can't know is an error, so it won't be reported):
Code:
	subl $4, %esp
	
	power_loop_start:
		imull -4(%ebp), 8(%ebp)
That subl allocates a local 32 bit value at -4(%ebp) but it doesn't initialize that value to anything. Then you seem to be trying to use the value as if it had been initialized.

Once you understand what operations are best or only doable in registers, I expect you would eliminate the use of -4(%ebp) from your code and use a register instead.

But in later projects, you may need to use memory for local variables.

Whether you use memory or registers for local variables, you should remember to initialize your local variables, just as you know you need to in C.

Last edited by johnsfine; 03-05-2010 at 12:42 PM.
 
Old 03-05-2010, 12:51 PM   #51
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I fixed the logic error.

Anyway, if you can't use some addressing modes for some operations, then how are you supposed to write good programs? In fact, my code is very similar to the power example in the book, and as shows errors on some parts that are the same!
 
Old 03-05-2010, 01:12 PM   #52
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MTK358 View Post
I fixed the logic error.

Anyway, if you can't use some addressing modes for some operations, then how are you supposed to write good programs? In fact, my code is very similar to the power example in the book, and as shows errors on some parts that are the same!
You need to understand that assembler's purpose is not convinient programing. That is what high level languages are for.
Assembler is used to program as close as possible to the CPU's abilities.
 
Old 03-05-2010, 01:16 PM   #53
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by crts View Post
You need to understand that assembler's purpose is not convinient programing. That is what high level languages are for.
Assembler is used to program as close as possible to the CPU's abilities.
I know that. I'm still just saying...

And where do I get this reference and how do I work around these limitations?
 
Old 03-05-2010, 01:34 PM   #54
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
...
And where do I get this reference and how do I work around these limitations?
Every CPU manufacturer issues quite detailed documentation describing all the peculiarities.

You haven't yet looked, into, say, ARM . ARM is actually not bad - much more RISCy, so easier to understand. OTOH, it can be both big and little endian, and IIRC it has so called thumb mode (16 bits instead of 32 bits).
 
Old 03-05-2010, 01:47 PM   #55
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
Every CPU manufacturer issues quite detailed documentation describing all the peculiarities.

You haven't yet looked, into, say, ARM . ARM is actually not bad - much more RISCy, so easier to understand. OTOH, it can be both big and little endian, and IIRC it has so called thumb mode (16 bits instead of 32 bits).
Maybe that's a good idea. Is there some sort of ARM emulator available? Perhaps even something that can run Linux?
 
Old 03-05-2010, 01:52 PM   #56
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I just found an article that describes how to set up QEMU to emulate an ARM machine and install Linux on it! Gotta try it!
 
0 members found this post helpful.
Old 03-05-2010, 01:54 PM   #57
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Maybe that's a good idea. Is there some sort of ARM emulator available? Perhaps even something that can run Linux?
ARM can be run under QEMU and there ready-made bootable Linux images - Debian. Use your favorite WEB search engine, something like

QEMU ARM Debian image

should suffice.

I did run ARM emulator myself - 'gcc' works in it. I.e. you can compile natively inside QEMU.
 
Old 03-05-2010, 02:13 PM   #58
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MTK358 View Post
I know that. I'm still just saying...

And where do I get this reference and how do I work around these limitations?
Have a look at page 263 of the book. There is a reference table of some common x86 instructions. Depending on what chip you intend to prgram you might have to contact the chip manufacturer.

As for the work around:
Again, this are physical limitations we are talking about. High level languages 'hide' the chip's specific architecture.
If you are still wondering, why you can't do something like

Code:
imull  -4(%ebp), 8(%ebp)
then keep in mind, that -4(%ebp) and 8(%ebp) point to just some memory locations. So it is just some circuits that are capable of holding some values. There is no cicuitry in your memory that can multiply this two dates. Only your CPU provides that functionality. So before you can operate on those values, your CPU has to fetch them first to its registers which have that special circuits to do a mutliplication and other logical operations.
 
Old 03-05-2010, 02:16 PM   #59
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MTK358 View Post
where do I get this reference and how do I work around these limitations?
I looked on AMD's web site for a good directory of the documents you should get. I've seen that directory before, but couldn't find it.

I can still find all the individual documents. These are documentation for x86_64 CPUs. They cover both the 32 bit and 64 bit instruction sets, because the CPUs themselves include both. But some of the explanation is pretty skimpy for 32 bit specific sub topics, because that is not what they want you to use. You might need to find older documentation for more detail on 32 bit. (But I still think you should switch to 64 bit asm).

The pdf with the basic instruction set documentation is
http://support.amd.com/us/Processor_TechDocs/24594.pdf

Some others you should get include 24592, 26568 and the two documents at
http://www.x86-64.org/documentation.html

Quote:
Originally Posted by MTK358 View Post
In fact, my code is very similar to the power example in the book, and as shows errors on some parts that are the same!
I figured out that part of your problem.

You already understood that the book was giving examples in 32 bit x86 asm and you know you have 64 bit Linux installed so the as command will default to 64 bit x86 asm. But you seem to have ignored that problem.

If you use as --32 instead of as it will assemble a program in 32 bit x86 asm.

The two asm languages have so much superficial similarity, that you got a trivial program written for 32 bit x86 asm to assemble and even run correctly in 64 bit x86 asm (even though each instruction did something subtly different). But you can't extend that to bigger projects. For example, 32 bit x86 asm has the pushl instruction to push a 32 bit value onto the stack. In 64 bit x86 asm, there is a lot more attention to stack alignment, so they intentionally prevented you from pushing 32 bits onto the stack. Regardless of the source size of the thing you want to push, it must be pushed onto the stack as 64 bits.

In memory references other than push/pop, the default integer size in x86_64 is still 32 bits, so there is no problem reading and writing 32 bit integers from/to memory, including from/to memory allocated on the stack and accessed through (%rsp) or (%rbp). You just can't push nor pop 32 bits.

So if you want to continue typing in 32 bit x86 asm examples, start assembling and linking them in 32 bit mode. (No special option is needed to run 32 bit in 64 bit Linux, just to assemble and link 32 bit).

I think you would learn more if you translated the examples to 64 bit x86 asm and assembled and linked the default way.

Last edited by johnsfine; 03-05-2010 at 02:38 PM.
 
Old 03-05-2010, 02:26 PM   #60
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
I can still find all the individual documents. These are documentation for x86_64 CPUs. They cover both the 32 bit and 64 bit instruction sets, because the CPUs themselves include both. But some of the explanation is pretty skimpy for 32 bit specific sub topics, because that is not what they want you to use. You might need to find older documentation for more detail on 32 bit. (But I still think you should switch to 64 bit asm).
Does 64 bit ASM work on a 32bit OS working on a 64 bit processor?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ASM or C++? Hb_Kai Programming 16 01-20-2010 09:12 AM
Is ASM dangerous? MrCode Programming 37 11-18-2009 08:29 AM
ASM x32 vs ASM x64 Tegramon Programming 3 02-27-2008 02:26 PM
I/O in ASM Mercurius Programming 10 11-16-2006 07:02 PM
ASM question zWaR Programming 2 06-26-2004 11:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration