Re: Question about jumps
- From: Herbert Kleebauer <klee@xxxxxxxxx>
- Date: Sun, 27 Aug 2006 16:37:05 +0200
Markus Pitha wrote:
Herbert Kleebauer wrote:
There is no need to put read only data in the data segment,
it is best placed in the code segment.
Ok, I'm still not absolutely sure about the differences between the
segments yet.
C uses NULL terminated strings, bur we are in assembly (and here we
decide ourselves how to terminate an string). Redirect the output to a
file and look at the hex dump of the file, then you will see, the 0 is
written as part of the string to stdout.
I see. I thought it's the same in assembly. So the last character of the
$-equ string tells assembler the end of the string, is that right?
Not the end of the string, but the length of the string:
textstart: dc "hello world"
textend:
textlength=textend-textstart
Or, instead of defining "textend" explicitly, you can use
the current location counter instead:
textlength=$-textstart
But then you have to write this immediately after the text,
so the current location counter has the same value as textend.
But for starting assembly programming in Linux, I would use
an assembler which can generate a flat outputfile (like NASM)
and don't use any linker . This way you really see what's
in your executable.
Here a simple example for a Linux executable. There are five
parts in the file:
ELF header: Don't modify this part, it is required by Linux to load
and execute the program.
code: Insert your code here, execution starts at the label main
(or whatever label is given in the ELF header)
constant data: Insert your read only data here
initialized data: Insert your initialized variables here
uninitialized data: Insert your uninitialized variables here
If you make a hex dump of the executable, you will see, there
is nothing than the bytes you explicitly defined in the source
code.
;===========================================================================
seg32
@=$08048000
code_offset=@@
code_addr:
;--------------------------- ELF header -----------------------------------
dc.l $464c457f,$00010101,0,0,$00030002,1,main,$34,0,0,$00200034,2,0
dc.l 1,code_offset,code_addr,code_addr,code_filez,code_memsz,5,4096
dc.l 1,data_offset,data_addr,data_addr,data_filez,data_memsz,6,4096
;--------------------------- code ------------------------------------------
main: move.l #1,r3 ; stdout
move.l #text,r2 ; text start
move.l #text_l,r1 ; text length
move.l #4,r0 ; write
trap #$80
move.l #0,r3 ; return code
move.l #1,r0 ; exit
trap #$80
;--------------------------- constant data ---------------------------------
text: dc.b "hello world",10
text_l=@-text
;---------------------------------------------------------------------------
code_filez=@@-code_offset
code_memsz= @-code_addr
even 4
@=(@+4095)/4096*4096+(@@\4096)
data_offset=@@
data_addr:
;--------------------------- initialized data ------------------------------
;var1: dc.l 1
;var2: dc.l 11
;--------------------------- uninitialized data ----------------------------
;var3: blk.l 1
;buf: blk.b 1000
;---------------------------------------------------------------------------
data_filez=@@-data_offset
data_memsz= @-data_addr
;===========================================================================
.
- References:
- Question about jumps
- From: Markus Pitha
- Re: Question about jumps
- From: Herbert Kleebauer
- Re: Question about jumps
- From: Markus Pitha
- Re: Question about jumps
- From: Herbert Kleebauer
- Re: Question about jumps
- From: Markus Pitha
- Question about jumps
- Prev by Date: Re: Question about jumps
- Next by Date: Re: beginner, explanation help with output
- Previous by thread: Re: Question about jumps
- Next by thread: Re: Question about jumps
- Index(es):
Relevant Pages
|