PDA

Visualizza la versione completa : [C] C e ASM


fedeg93
14-01-2008, 15:45
perche se faccio un programma ad esempio :



int main(){
printf ("%s","Ciauuu - Benvenuto in GalatolOS !\n");
printf ("%s","Come Ti Chiami : ");
char nome[500];
scanf("%s",nome);
printf ("%s","Ciao ",nome);
}



e poi prendo il codice assembly con GDB



federico@crash-overflow:~/GalatolOS$ gdb main
GNU gdb 6.6-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) disass main
Dump of assembler code for function main:
0x08048208 <main+0>: lea 0x4(%esp),%ecx
0x0804820c <main+4>: and $0xfffffff0,%esp
0x0804820f <main+7>: pushl 0xfffffffc(%ecx)
0x08048212 <main+10>: push %ebp
0x08048213 <main+11>: mov %esp,%ebp
0x08048215 <main+13>: push %ecx
0x08048216 <main+14>: sub $0x214,%esp
0x0804821c <main+20>: mov %gs:0x14,%eax
0x08048222 <main+26>: mov %eax,0xfffffff8(%ebp)
0x08048225 <main+29>: xor %eax,%eax
0x08048227 <main+31>: movl $0x809e4e8,(%esp)
0x0804822e <main+38>: call 0x8048cf0 <puts>
0x08048233 <main+43>: movl $0x809e50a,0x4(%esp)
0x0804823b <main+51>: movl $0x809e51c,(%esp)
0x08048242 <main+58>: call 0x8048c90 <printf>
0x08048247 <main+63>: lea 0xfffffe04(%ebp),%eax
0x0804824d <main+69>: mov %eax,0x4(%esp)
0x08048251 <main+73>: movl $0x809e51c,(%esp)
0x08048258 <main+80>: call 0x8048cc0 <scanf>
0x0804825d <main+85>: lea 0xfffffe04(%ebp),%eax
0x08048263 <main+91>: mov %eax,0x8(%esp)
0x08048267 <main+95>: movl $0x809e51f,0x4(%esp)
---Type <return> to continue, or q <return> to quit---
0x0804826f <main+103>: movl $0x809e51c,(%esp)
0x08048276 <main+110>: call 0x8048c90 <printf>
0x0804827b <main+115>: mov 0xfffffff8(%ebp),%edx
0x0804827e <main+118>: xor %gs:0x14,%edx
0x08048285 <main+125>: je 0x804828c <main+132>
0x08048287 <main+127>: call 0x8050d70 <__stack_chk_fail>
0x0804828c <main+132>: add $0x214,%esp
0x08048292 <main+138>: pop %ecx
0x08048293 <main+139>: pop %ebp
0x08048294 <main+140>: lea 0xfffffffc(%ecx),%esp
0x08048297 <main+143>: ret
End of assembler dump.
(gdb)



lo metto in un file (boot.asm) non va?

boot.asm:


lea 0x4(%esp),%ecx
and $0xfffffff0,%esp
pushl 0xfffffffc(%ecx)
push %ebp
mov %esp,%ebp
push %ecx
sub $0x214,%esp
mov %gs:0x14,%eax
mov %eax,0xfffffff8(%ebp)
xor %eax,%eax
movl $0x809e4e8,(%esp)
call 0x8048cf0 <puts>
movl $0x809e50a,0x4(%esp)
movl $0x809e51c,(%esp)
call 0x8048c90 <printf>
lea 0xfffffe04(%ebp),%eax
mov %eax,0x4(%esp)
movl $0x809e51c,(%esp)
call 0x8048cc0 <scanf>
lea 0xfffffe04(%ebp),%eax
mov %eax,0x8(%esp)
movl $0x809e51f,0x4(%esp)
movl $0x809e51c,(%esp)
call 0x8048c90 <printf>
mov 0xfffffff8(%ebp),%edx
xor %gs:0x14,%edx
je 0x804828c <main+132>
call 0x8050d70 <__stack_chk_fail>
add $0x214,%esp
pop %ecx
pop %ebp
lea 0xfffffffc(%ecx),%esp
ret


PS. esiste un altro programma per prendere il codice assembly???

GRAZIEEEE

pallinopinco
14-01-2008, 15:57
Chiedi il codice assembly al tuo compilatore GNU GCC:

gcc nomeprogramma.c -save-temps -O2 -s

Le ultime 2 opzioni sono facoltative.

Esempio:



#include <stdio.h>

int main(void){
puts("CIAO!");
return 0;
}




.file "test.c"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "CIAO!\0"
.text
.p2align 4,,15
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $4, %esp
call ___main
movl $LC0, (%esp)
call _puts
addl $4, %esp
xorl %eax, %eax
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.def _puts; .scl 2; .type 32; .endef



Ovviamente puoi assemblare il sorgente Assembly con i programmi della toolchain GNU.

fedeg93
14-01-2008, 16:07
Di male in peggio....

ho fatto come hai detto te ma non va....



boot.asm:1: error: attempt to define a local label before any non-local labels
boot.asm:1: error: parser: instruction expected
boot.asm:2: error: attempt to define a local label before any non-local labels
boot.asm:2: error: parser: instruction expected
boot.asm:3: error: attempt to define a local label before any non-local labels
boot.asm:3: error: parser: instruction expected
boot.asm:4: error: attempt to define a local label before any non-local labels
boot.asm:5: error: attempt to define a local label before any non-local labels
boot.asm:5: error: parser: instruction expected
boot.asm:6: error: symbol `.section' redefined
boot.asm:6: error: parser: instruction expected
boot.asm:7: error: attempt to define a local label before any non-local labels
boot.asm:8: error: symbol `.string' redefined
boot.asm:8: error: parser: instruction expected
boot.asm:9: error: attempt to define a local label before any non-local labels
boot.asm:10: error: symbol `.string' redefined
boot.asm:10: error: parser: instruction expected
boot.asm:11: error: attempt to define a local label before any non-local labels
boot.asm:12: error: symbol `.string' redefined
boot.asm:12: error: parser: instruction expected
boot.asm:13: error: attempt to define a local label before any non-local labels
boot.asm:14: error: attempt to define a local label before any non-local labels
boot.asm:14: error: parser: instruction expected
boot.asm:15: error: attempt to define a local label before any non-local labels
boot.asm:15: error: parser: instruction expected
boot.asm:16: error: attempt to define a local label before any non-local labels
boot.asm:16: error: parser: instruction expected
boot.asm:18: error: parser: instruction expected
boot.asm:19: error: parser: instruction expected
boot.asm:20: error: parser: instruction expected
boot.asm:21: error: symbol `pushl' redefined
boot.asm:21: error: parser: instruction expected
boot.asm:22: error: parser: instruction expected
boot.asm:23: error: parser: instruction expected
boot.asm:24: error: symbol `movl' redefined
boot.asm:24: error: parser: instruction expected
boot.asm:25: error: symbol `movl' redefined
boot.asm:25: error: parser: instruction expected
boot.asm:26: error: symbol `leal' redefined
boot.asm:26: error: parser: instruction expected
boot.asm:27: error: symbol `movl' redefined
boot.asm:27: error: parser: instruction expected
boot.asm:28: error: symbol `movl' redefined
boot.asm:28: error: parser: instruction expected
boot.asm:29: error: parser: instruction expected
boot.asm:30: error: symbol `movl' redefined
boot.asm:30: error: parser: instruction expected
boot.asm:32: error: symbol `movl' redefined
boot.asm:32: error: parser: instruction expected
boot.asm:33: error: symbol `movl' redefined
boot.asm:33: error: parser: instruction expected
boot.asm:35: error: symbol `movl' redefined
boot.asm:35: error: parser: instruction expected
boot.asm:36: error: symbol `movl' redefined
boot.asm:36: error: parser: instruction expected
boot.asm:38: error: symbol `movl' redefined
boot.asm:38: error: parser: instruction expected
boot.asm:39: error: symbol `movl' redefined
boot.asm:39: error: parser: instruction expected
boot.asm:40: error: symbol `movl' redefined
boot.asm:40: error: parser: instruction expected
boot.asm:42: error: symbol `movl' redefined
boot.asm:42: error: parser: instruction expected
boot.asm:43: error: symbol `xorl' redefined
boot.asm:43: error: parser: instruction expected
boot.asm:45: error: symbol `movl' redefined
boot.asm:45: error: parser: instruction expected
boot.asm:46: error: symbol `movl' redefined
boot.asm:46: error: parser: instruction expected
boot.asm:47: error: symbol `movl' redefined
boot.asm:47: error: parser: instruction expected
boot.asm:48: error: parser: instruction expected
boot.asm:49: error: symbol `leal' redefined
boot.asm:49: error: parser: instruction expected
boot.asm:53: error: parser: instruction expected
boot.asm:54: error: parser: instruction expected
boot.asm:55: error: parser: instruction expected

pallinopinco
14-01-2008, 16:24
Con quale assembler cerchi di assemblare il sorgente?

Posso garantirti che con GNU AS funziona alla perfezione...

Loading