简体   繁体   中英

x86 assembly program does not execute

I'm new to assembly tell me what's wrong with this, I don't know how to solve this issue.

section    .text
    global _start   
_start:             
    mov edx,len     
    mov ecx,msg     
    mov ebx,1       
    mov eax,4       

    mov eax,1               

section .data
msg db 'Hello, world!', 0xa  ;
len equ $ - msg     ;

As already said your program is obviosly missing the system calls.

Under Windows (as far as you are generating an .EXE file for Windows) there are no direct system calls. Instead you'll have to call functions in the DLLs that come with Windows.

Example:

mov ecx,len
push ecx
mov ecx,msg
push ecx
mov ecx,1
push ecx
call _write
add esp,12

In 32-bit windows (or 32-bit programs running in 64-bit windows) there are basically two types of functions: stdcall (= WINAPI, CALLBACK, PASCAL) and cdecl.

For both types of functions the arguments must be on the stack (the first argument must be at ESP+0, the second one at ESP+4 and so on) so you "push" the arguments while the last one is pushed first (in the example "1" is the first one and "len" is the last one). The result of the function (if any) is returned in the EAX register (as long as it is not a floating point value).

Then you call the function which is defined in a DLL. In the end that function will do a system call however this system call may be version specific! You do not have to care about that.

In the case of "stdcall" functions the function will remove the arguments from the stack. (For functions with a variable number of arguments - like wsprintf - only the mandatory arguments are removed.) In assembler such functions are named:

_Name@nnn

while "Name" is the name of the function as known in C language and nnn is the number of bytes that will be removed from the stack. Most low-level Windows functions are stdcall functions. Note that functions that use strings typically end with an "A" to indicate ASCII or "W" to indicate UNICODE strings. You typically use the "A" variant. Example: Calling the function "MessageBox":

(push 4 arguments)
call _MessageBoxA@16

Most C standard library functions are "cdecl" - see the example of "write" above. Cdecl functions do not adjust the stack pointer so you'll have to add an "add esp,nnn" after the "call" instruction. The name is simply formed by adding the C function name after an underscore (eg "write()" -> "call _write").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM