Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

x86 - assembly system call non-effective

I want to print AAAA with the following:

BITS 32;

;write;
 push 0x41414141;
 pop ecx        ;
 mov eax, 4     ; write is syscall 4 for Ubuntu 32-bit
 mov ebx, 1     ; stdout
 mov edx, 4     ;
 int 0x80       ;

;exit;
 mov eax, 1     ;
 mov ebx, 0     ;
 int 0x80       ;

Yet, once assembled and linked this code only exits, no errors, what is wrong ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A quick fix of your code:

push 0x41414141 ; put 'AAAA' into stack memory
mov ecx,esp     ; pointer to the 'AAAA'
mov eax, 4      ; write is syscall 4 for 32-bit Linux
mov ebx, 1      ; stdout
mov edx, 4
int 0x80
add esp,4      ; restore stack

No explanation, as you should first check what I did ask in comment, then the fix will be either obvious, or you will have to ask about something particular you don't understand...

If you run your original code with strace ./my_program, you'd see write() return -EFAULT because you passed a bad address. Always use strace to debug programs that make syscalls and don't behave the way you expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...