* redirection
@ 2006-03-01 13:37 glburt
2006-03-01 14:48 ` redirection Marcin Kościelnicki
0 siblings, 1 reply; 2+ messages in thread
From: glburt @ 2006-03-01 13:37 UTC (permalink / raw)
To: linux-assembly
I have the following program:
SECTION .data ; data section
msg1: db "Hello World -- 1",10,0 ; the string to print, 10=cr
msg: db "Hello World",10 ; the string to print, 10=cr
len: equ $-msg ; "$" means "here"
; len is a value, not an address
SECTION .text ; code section
global main ; make label available to linker
extern printf
main: ; standard gcc entry point
mov edx,len ; arg3, length of string to print
mov ecx,msg ; arg2, pointer to string
mov ebx,1 ; arg1, where to write, screen
mov eax,4 ; write command to int 80 hex
int 0x80 ; interrupt 80 hex, call kernel
push msg1
call printf
add esp, 4
mov ebx,0 ; exit code, 0=normal
mov eax,1 ; exit command to kernel
int 0x80 ; interrupt 80 hex, call kernel
When I assembled/linked it, I used:
nasm -f elf burtp1.asm
gcc burtp1.o
when i ran it, I got two lines of output.:
[burt@linux2 ~]$ ./a.out
Hello World
Hello World -- 1
When I redirected it, I got:
[burt@linux2 ~]$ ./a.out > foo
[burt@linux2 ~]$ cat foo
Hello World
Why was the output from using printf not redirected?
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: redirection
2006-03-01 13:37 redirection glburt
@ 2006-03-01 14:48 ` Marcin Kościelnicki
0 siblings, 0 replies; 2+ messages in thread
From: Marcin Kościelnicki @ 2006-03-01 14:48 UTC (permalink / raw)
To: linux-assembly
> I have the following program:
>
> SECTION .data ; data section
> msg1: db "Hello World -- 1",10,0 ; the string to print, 10=cr
> msg: db "Hello World",10 ; the string to print, 10=cr
> len: equ $-msg ; "$" means "here"
> ; len is a value, not an address
>
> SECTION .text ; code section
> global main ; make label available to linker
> extern printf
> main: ; standard gcc entry point
>
> mov edx,len ; arg3, length of string to print
> mov ecx,msg ; arg2, pointer to string
> mov ebx,1 ; arg1, where to write, screen
> mov eax,4 ; write command to int 80 hex
> int 0x80 ; interrupt 80 hex, call kernel
>
> push msg1
> call printf
> add esp, 4
>
> mov ebx,0 ; exit code, 0=normal
> mov eax,1 ; exit command to kernel
> int 0x80 ; interrupt 80 hex, call kernel
>
>
> When I assembled/linked it, I used:
>
> nasm -f elf burtp1.asm
> gcc burtp1.o
>
> when i ran it, I got two lines of output.:
>
> [burt@linux2 ~]$ ./a.out
> Hello World
> Hello World -- 1
>
>
> When I redirected it, I got:
>
> [burt@linux2 ~]$ ./a.out > foo
> [burt@linux2 ~]$ cat foo
> Hello World
>
> Why was the output from using printf not redirected?
It's because printf buffers output when it goes to file.
In c program, it cannot do any wrong, since library exit() function flushes
all buffers before actually exitting.
However, you exit with direct kernel syscall. It's REALLY bad idea, as it
terminates the process immediately, not allowing anything to run before it
happens -- including the flush-buffers-on-exit thingie.
If you're not using libc, direct syscall is alright. However, if you use libc,
you have to use the whole framework, or something's bound to break
eventually. Will people never learn?
To make it do the Right Thingâ„¢, change this:
> mov ebx,0 ; exit code, 0=normal
> mov eax,1 ; exit command to kernel
> int 0x80 ; interrupt 80 hex, call kernel
to this:
push dword 0 ;exit code
call exit
Should work now.
----------------------------------------------------------------------
Kliknij po wiecej! >>> http://link.interia.pl/f18ed
-
To unsubscribe from this list: send the line "unsubscribe linux-assembly" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2006-03-01 14:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-01 13:37 redirection glburt
2006-03-01 14:48 ` redirection Marcin Kościelnicki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).