Reputation: 150
segment .data
msg db "Enter your ID", 0xA, 0xD
len equ $ - msg
segment .bss
id resb 10
segment .text
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, len
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, id
mov edx, 10
int 0x80
mov eax, 4
mov ebx, 1
int 0x80
_exit:
mov eax, 1;
xor ebx, ebx
int 0x80
;End
I am trying to compile this file in c using gcc, but the program gives me an error and I have absolutely no idea where the problem is. Does it have anything to do with my OS?
Upvotes: 6
Views: 7369
Reputation: 113
This program will work only in 32 bit Linux . Still there are issues in this program.
Change _start
to main
Also, ecx
and edx
might not be preserved after a system call (int 0x80
)
Please try the below example.
Assemble & link with:
nasm -felf hello.asm
gcc -o hello hello.o
code:
segment .data
msg db "Enter your ID", 0xA
len equ $ - msg
segment .bss
id resb 10
segment .text
global main
main:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, len
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, id
mov edx, 10
int 0x80
mov edx, eax ;; length of the string we just read in.
mov eax, 4
mov ebx, 1
mov ecx, id
int 0x80
_exit:
mov eax, 1;
xor ebx, ebx
int 0x80
;End
Upvotes: 3
Reputation: 54
I know this is an old thread, but I have this issue all the time and want to clarify it.
You need to change _start
to _main
. I believe this is so NASM can assemble your file correctly, replacing references to WinMain@16
with _main
so that MinGW can link it successfu
Also, int 0x80
is a LINUX system call. Perhaps use call _printf
instead, and put extern _printf
at the top of your program.
Upvotes: 1
Reputation: 113
For Windows, if you are OK to use C libraries, try this example.
;;Assemble and link with
;nasm -fwin32 hello.asm
;gcc -o hello hello.obj
global _main
extern _scanf
extern _printf
segment .data
msg: db "Enter your ID", 0xA, 0xD, 0 ; note the null terminator.
formatin: db "%s", 0 ; for scanf.
segment .bss
id resb 10
segment .text
_main:
push msg
call _printf
add esp, 4
push id ; address of number1 (second parameter)
push formatin ; arguments are pushed right to left (first parameter)
call _scanf
add esp, 8
push id
call _printf
add esp,4
ret
Upvotes: 1
Reputation: 21507
Your code is not supposed to use standard C library, so linking it with bare ld
instead of gcc
would help (_start
is the entry point by default, other one can be specified with --entry
option for ld
).
But it won't help: this code is not for Windows OS, and you're obviously compiling it for Windows.
Upvotes: 0