Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...