News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

mov eax,4

Started by Napsteren, November 22, 2010, 08:25:32 PM

Previous topic - Next topic

Napsteren

Hello MASM

I'm new in this assembly world, and right now I'm just learning how to program in the language. I just need to know 1 simple thing, here we go.

mov eax, 4

Why the value 4?

Best regards,

Napsteren

oex

:lol That's a tough one.... do you have any context to your problem?
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Napsteren

SECTION .data         ; Section containing initialised data
                            
EatMsg: db "Eat at Joe's!",10
     
EatLen: equ $-EatMsg   
                           
SECTION .bss         ; Section containing uninitialized data   
                               
SECTION .text         ; Section containing code
                                 
_start         ; Linker needs this to find the entry point!
                                
_start:

nop         ; This no-op keeps gdb happy...

mov eax,4      ; Specify sys_write call

mov ebx,1      ; Specify File Descriptor 1: Standard Output

mov ecx,EatMsg      ; Pass offset of the message

mov edx,EatLen      ; Pass the length of the message

int 80H         ; Make kernel call
                         
MOV eax,1      ; Code for Exit Syscall

mov ebx,0      ; Return a code of zero   

int 80H         ; Make kernel call
______________________

I just need to know why it needs to be 4, or 1?

It would be nice if you would tell me all the "why" quetions in this code :p Like why ( ,10 "line 2 )
                 

redskull

It's an argument to specifiy which function is performed by the INT.  Though what you are doing looks like Linux, not Windows.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

Gunther

Quote from: redskull, November 22, 2010, at 08:39:18 PMThough what you are doing looks like Linux, not Windows.

Exactly.

Quote from: Napsteren, November 22, 2010, 08:25:32 PMWhy the value 4?

You have to specify a function number, for example, SYS_WRITE (=4); so you can write via handle into a file. Under Unix (Linux, BSD, SCO etc.) all is a file - the printer is a file, too; that's the Unix philosophy. Depending what you would like to do, you have to pass the function number and mostly some other parameters in registers. With INT 80h you wake up the operating system kernel, which will perform the specified action.

Gunther
Forgive your enemies, but never forget their names.

brethren

i recognise that example from jeff duntemanns 'assembly language step-by-step programming for linux'. read the book, don't just run examples, and you'll know what a system call is :P

QuoteIt would be nice if you would tell me all the "why" quetions in this code :p Like why ( ,10 "line 2 )

heres a clue. look it up using an ascii table

Napsteren

brethren, your right :D :p But I'm only at page 150, but I'm getting better :p :D

So what you mean is that every number like 4 has a specific funktion??



brethren

in linux you use system calls for using the functions the kernel provides, whereas with windows you use the win32 api.
heres a list of the system calls in linux. it'll tell you what values to load in what registers then just int 80h to make the call
http://bluemaster.iu.hio.no/edu/dark/lin-asm/syscalls.html

you can also use the c library, that'll come later in the book

ps in that first program you use sys_write and sys_exit, so look them up in the table and then examine the registers you used in the program

clive

Quote from: Napsteren
So what you mean is that every number like 4 has a specific funktion??

Or to turn that around, "every function has a specific number" so as to identify and discriminate it from other functions with different numbers.
It could be a random act of randomness. Those happen a lot as well.

Napsteren

Omg! It all makes sense now :p xD

Thanks guys!

Here's what it is:

Every register, like EAX, got a specifi number for a specifi funktion. Like mov EAX, 4 where 4 is the sys_write??

Am I getting it right here?

brethren

yes, when you make a system call eax contains the number of the call, ebx, ecx, edx will contain the arguments. so sys_exit is mov eax, 1 and you need to provide an argument (the value your program returns just like using C) mov ebx, 0 and then make the call int 80h

btw this is the windows masm users forum. if you go here
http://forum.nasm.us/
you'll find linux nasm users  :U

Napsteren

I just want to say thanks! You know that i just went from here  :eek to  :U

1 last thing then :D

So what your saying is that after i made a sys_call i need to give it an aragument with even ebx, ecx or edx?

Like:

mov eax, 4

mov ebx, 0


I got it now! So the 0 means return 0? like nothing xD?

Sorry, but i know Im a little annyoing right now, asking all these obvious questions to you :p But i think its awesome than you wanna help!

This is going to be my last post here then ;)

brethren

obviously each function requires different arguments, so the example you just posted syscall 4

eax=4 sys_write (for writing to files)
ebx=1 file descriptor for stdout (standard output remember in linux 'everythings a file'. Linux uses files as abstractions of devices)
ecx=the address of the string you want to output to the screen
edx=the number of characters in the string

to return 0 from your program you'd

eax=1 sys_exit
ebx=number to return (0 if the program exited normally like c's return 0)

QuoteSo what your saying is that after i made a sys_call i need to give it an aragument with even ebx, ecx or edx?

you put the values in the registers BEFORE you make the syscall (int 80h)

Napsteren

Yes, i understand it now ty mate! :)

So it all depends on the EAX and function call right? Like if the eax func call is this, then the ebx needs that, and ecx this and so on.


Gunther

Quote from: Napsteren, November 22, 2010, at 11:20:34 pmSo it all depends on the EAX and function call right? Like if the eax func call is this, then the ebx needs that, and ecx this and so on.

Yes. Please check the link which brethren provided above. That should help for getting further information.

Gunther
Forgive your enemies, but never forget their names.