Re: newbie: I/O with nasm



TK schrieb:

Hi,

how can I do simple I/O with nasm? For example: I want to read a string
from the commandline. Please keep it simple;-).

As an example, say you run a program called program and give it three arguments:

../program foo bar 42

The stack will then look as follows:
4
program
foo
bar
42

Number of arguments (argc), including the program name
Name of the program (argv[0])
Argument 1, the first real argument (argv[1])
Argument 2 (argv[2])
Argument 3 (argv[3]) (Note: this is the string "42", not the number 42)

Now lets write the program program that takes the three arguments:

section .text
global _start

_start:
pop eax ; Get the number of arguments
pop ebx ; Get the program name
pop ebx ; Get the first actual argument ("foo")
pop ecx ; "bar"
pop edx ; "42"

mov eax,1
mov ebx,0
int 80h ; Exit

After all that popping, EAX contains the number of arguments, EBX points to
wherever "foo" is stored in memory, ECX points to "bar" and EDX to "42". This is
obviously way more elegant and simple than in DOS. It took us just 5 lines to get
the arguments and even how many there are, while in DOS it takes 14 rather
complicated lines just to get one argument! Note that the 3rd pop overwrites the
value we put in EBX with the 2nd pop (which was the program name). Unless you have
a really good reason, you can usually chuck away the program name as we did here.

Dirk
.



Relevant Pages

  • Re: which way is faster?
    ... So what you ask me here would be a tiny OS on top of DOS. ... alternatives to unless using hardware acceleration. ... PUSH ebx ... MOV edx,01000;scan line size ...
    (alt.lang.asm)
  • Re: which way is faster?
    ... So what you ask me here would be a tiny OS on top of DOS. ... PUSH ebx ... MOV edx,01000;scan line size ... EMM and XMS wont do well here, because IRQs become disabled for too ...
    (alt.lang.asm)