Pagina 1 di 1

[ASM] A che serve addl??

Inviato: gio 9 apr 2009, 12:13
da Plaoo
Salve a tutti sto leggendo un manuale di programmazione assembler su linux (quello di Claudio Daffra) ho questo esempio:

Codice: Seleziona tutto

#Primo programma

#SEZIONE DATI
.section .data
output_string:
.ascii "Hello world\n\0"
#SEZIONE DATI FINE

#SEZIONE CODICE
.section .text

.globl _start

_start:

pushl           $output_string
call               printf
addl             $4,%esp ;<-------a che serve??

pushl           $0
call              exit
#SEZIONE CODICE FINE
Qualcuno mi può spiegare a che serve quel addl sul registro dello stack?? Ho fatto altri programmi con stringhe più lunghe e variabili "spinte" e "riprese" dallo stack senza usare addl e non è successo niente i programmi funzionano benissimo lo stesso.

Re: [ASM] A che serve addl??

Inviato: gio 9 apr 2009, 12:32
da Naitso
dopo il primo esempio trovi la riga:
addl (trattazione estesa nel capitolo: Aritmetica e Logica 2)
nel capitolo 5 è spiegato
Ciao

Re: [ASM] A che serve addl??

Inviato: gio 9 apr 2009, 12:45
da Plaoo
Naitso ha scritto:dopo il primo esempio trovi la riga:
addl (trattazione estesa nel capitolo: Aritmetica e Logica 2)
nel capitolo 5 è spiegato
Ciao
Certo ho controllato, quindi so che somma ma perchè c'è bisogno di farlo??

Re: [ASM] A che serve addl??

Inviato: gio 9 apr 2009, 13:25
da Mario Vanoni
In

http://linuxgazette.net/issue94/ramankutty.html

5. Using The Stack

A section of your program's memory is reserved for use as a stack. The Intel 80386 and above microprocessors contain a register called stack pointer, esp, which stores the address of the top of stack. Figure 1 below shows three integer values, 49,30 and 72, stored on the stack (each integer occupying four bytes) with esp register holding the address of the top of stack.
Figure 1

Unlike the stack analogous to a pile of bricks growing up wards, on Intel machines stack grows down wards. Figure 2 shows the stack layout after the execution of the instruction pushl $15.
Figure 2

The stack pointer register is decremented by four and the number 15 is stored as four bytes at locations 1988, 1989, 1990 and 1991.

The instruction popl %eax copies the value at top of stack (four bytes) to the eax register and increments esp by four. What if you do not want to copy the value at top of stack to any register? You just execute the instruction addl $4, %esp which simply increments the stack pointer.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In Listing 3, the instruction call foo pushes the address of the instruction after the call in the calling program on to the stack and branches to foo. The subroutine ends with ret which transfers control to the instruction whose address is taken from the top of stack. Obviously, the top of stack must contain a valid return address.

Anche se non ho mai usato asm.

Re: [ASM] A che serve addl??

Inviato: gio 9 apr 2009, 16:32
da phobos3576
Come ha già scritto Mario Vanoni, si tratta della convenzione seguita dal linguaggio C per la pulizia dello stack quando si esce da un procedura (o funzione); tale convenzione è opposta a quella del linguaggio Pascal.

Prima di tutto, bisogna ricordare che, secondo la sintassi (bella o brutta, a seconda dei gusti) dell'Assembly AT&T:

l = long = 32 bit
w = word = 16 bit
b = byte = 8 bit

Convenzione Pascal:

Codice: Seleziona tutto

pushl    $output_string ; inserisce un parametro a 32 bit (4 byte) nello stack
call     printf         ; chiama la procedura printf

; Qui il programma prosegue dopo l'uscita dalla procedura printf
; Lo stack viene pulito automaticamente dall'istruzione RET all'uscita da printf
Convenzione C:

Codice: Seleziona tutto

pushl    $output_string  ; inserisce un parametro a 32 bit (4 byte) nello stack
call     printf          ; chiama la procedura printf
addl     $4,%esp         ; pulizia dello stack (4 byte) DOPO l'uscita da printf

Re: [ASM] A che serve addl??

Inviato: gio 9 apr 2009, 23:08
da Plaoo
Grazie ragazzi siete stati molto chiari!!! :thumbright: