LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2015, 11:59 AM   #1
billvance
Member
 
Registered: Sep 2013
Location: Bothell, Washington USA
Distribution: Kubuntu 12.04
Posts: 60

Rep: Reputation: Disabled
How can I use pipes in assembly?


Howdy;

I'm currently running a 32-bit amd sempron, which with a
little luck, I'll be upgrading to 64-bits soon, In any
case, does anyone have some sample assembler code that
shows how to read from/write to pipes? Both 32 and 64
bit asm source would be appreciated.

Thanks.

Bill
 
Old 03-03-2015, 12:02 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
gcc -S my_pipe_demo.c
 
Old 03-03-2015, 12:14 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Basically, you use linux SVC's both in C and Assembly, so the main difference is that Assembly-code is harder to write, but you cannot use it if you change platform.
 
Old 03-03-2015, 12:21 PM   #4
billvance
Member
 
Registered: Sep 2013
Location: Bothell, Washington USA
Distribution: Kubuntu 12.04
Posts: 60

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Basically, you use linux SVC's both in C and Assembly, so the main difference is that Assembly-code is harder to write, but you cannot use it if you change platform.
SVC? Whats that?

Bill
 
Old 03-03-2015, 12:35 PM   #5
genss
Member
 
Registered: Nov 2013
Posts: 744

Rep: Reputation: Disabled
a hello world example that comes with FASM, for amd64
32bit pipe example below
Code:
; fasm demonstration of writing 64-bit ELF executable
; (thanks to František Gábriš)

; syscall numbers: /usr/src/linux/include/asm-x86_64/unistd.h
; parameters order:
; r9    ; 6th param
; r8    ; 5th param
; r10   ; 4th param
; rdx   ; 3rd param
; rsi   ; 2nd param
; rdi   ; 1st param
; eax   ; syscall_number
; syscall

format ELF64 executable 3

segment readable executable

entry $

	mov	edx,msg_size	; CPU zero extends 32-bit operation to 64-bit
				; we can use less bytes than in case mov rdx,...
	mov	rsi, msg
	mov	edi,1		; STDOUT
	mov	eax,1		; sys_write
	syscall

	xor	edi,edi 	; exit code 0
	mov	eax,60		; sys_exit
	syscall

segment readable writeable

msg db 'Hello 64-bit world!',0xA
msg_size = $-msg
it demonstrates write() and exit() system calls

man 2 pipe tells you the argument pipe() takes is room for two file descriptors
a full example for 32bit linux:
Code:
format ELF executable 3
entry start

segment readable executable

start:

	;make pipe
	mov	ebx,fds ;pointer to a place where to store the resulting fd's
	mov	eax,42 ;sys_pipe
	int	0x80

	;send 'Hello world!',0xA to fd1
	mov	edx,msg_size
	mov	ecx,msg
	mov	ebx,[fd2]
	mov	eax,4 ;sys_write
	int	0x80

	;read msg from fd2
	mov	edx,256
	mov	ecx,tmp
	mov	ebx,[fd1]
	mov	eax,3 ;sys_read
	int	0x80
	mov	[pipe_msg_len], eax

	mov	edx,[pipe_msg_len]
	mov	ecx,tmp
	mov	ebx,1 ;stdout
	mov	eax,4 ;sys_write
	int	0x80

	mov	eax,1
	xor	ebx,ebx
	int	0x80

segment readable writeable

msg db 'Hello world!',0xA
msg_size = $-msg

fds:		;pointer to bout fds, fd's are 4byte each
fd1 rd 1	;place for fd1, read end of the pipe
fd2 rd 1	;place for fd2, write end of the pipe

tmp rb 256	;tmp piece of memory, 256 bytes

pipe_msg_len rd 1
as man 2 pipe says, you write to the second fd and that gets sent to the first fd (other way around does not work)


if something is not clear, ask
i'd advise to read the fasm manual first until you fully understand the hello world example
then to read the pipe() man page

edit: sorry, that you don't have a 64bit cpu went over my head
i'l make a 32bit example, just a sec
edit2: made it, hf

Last edited by genss; 03-03-2015 at 01:11 PM. Reason: example
 
1 members found this post helpful.
Old 03-03-2015, 01:04 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
SVC means 'Supervisor Call'; also known as 'system call' or 'syscall'. Meaning: the user program asks the kernel (or with another word: the executive) to do something.
 
Old 03-03-2015, 01:17 PM   #7
genss
Member
 
Registered: Nov 2013
Posts: 744

Rep: Reputation: Disabled
updated post above with a 32bit example


Quote:
Originally Posted by NevemTeve View Post
SVC means 'Supervisor Call'; also known as 'system call' or 'syscall'. Meaning: the user program asks the kernel (or with another word: the executive) to do something.
syscall or system call, the kernel isn't a supervisor (in this case, at least)
 
Old 03-03-2015, 02:36 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Off: The same thing might go under different names: supervisor, executive, kernel...)
 
Old 03-03-2015, 03:51 PM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by billvance View Post
some sample assembler code that
shows how to read from/write to pipes? Both 32 and 64
bit asm source would be appreciated.
That question tells me you know little if any x86 assembler.

The code you asked for is a bad idea, but still quite easy. So if you knew a little bit about x86 assembler, you would not have needed to ask. But if you don't know even a little about x86 assembler then you probably don't know that what you are asking for is a pointless sidetrack from learning any.

Do you know any C or C++ ?

If yes to either, then the best place to start learning assembler is learning to write C-callabe functions in assembler (not whole programs) that do computations (not I/O).

That is not just the right point of view for learning assembler. It is the right point of view for any real use of asembler.

Learning the basic framework for asm code to be a Linux program is cute bit of esoteric info. But is a waste of effort for fundamentally useless info. Write the framework of a program in some language that allows calling functions via a C interface then write your asm code as functions called by that interface.

Learning how to do Linux I/O directly from asm is similarly a waste of effort. It isn't useful in itself and it doesn't help you with anything else. If your asm code needs to do I/O have it call a C function that does the I/O.

But learning how to call C-callable functions from asm and learning how to write a C-callable function in asm has real value. First it is the kind of thing you would do in asm if you had a real reason to use asm. But maybe more importantly, it will give you some real extra insight into the important "under the hood" functioning of every program you write in other languages. It will make you better at designing and debugging high level language programs.

Quote:
Originally Posted by linosaurusroot View Post
gcc -S my_pipe_demo.c
That is a slightly flawed suggestion in the context of the original question, but a very very important suggestion if the OP instead starts to learn asm the right way.

Some of the rules of function calling especially parameter passing in x86-64 are a bit strange and are documented at a highly precise and abstract level, which makes them nearly impossible to understand if you don't already know the basic ideas. Learning by example is much better than from formal rules anyway.

Write a C function that calls another C function passing some parameters. Compile it with gcc -S and look at how to sets up the parameters and makes the call. Write the function it calls and have it do something trivial with the parameters. Look at the generated asm for that and see how a C-callable function is organized and how it gets its parameters etc.

Later read the official rules (for 32-bit you can easily infer the rules from a few examples. But for 64-bit you won't infer the rules from examples nor understand the rules without examples. You need both).

Last edited by johnsfine; 03-03-2015 at 04:00 PM.
 
Old 03-03-2015, 04:04 PM   #10
billvance
Member
 
Registered: Sep 2013
Location: Bothell, Washington USA
Distribution: Kubuntu 12.04
Posts: 60

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by genss View Post
a hello world example that comes with FASM, for amd64
32bit pipe example below
Code:
; fasm demonstration of writing 64-bit ELF executable
; (thanks to František Gábriš)

; syscall numbers: /usr/src/linux/include/asm-x86_64/unistd.h
; parameters order:
; r9    ; 6th param
; r8    ; 5th param
; r10   ; 4th param
; rdx   ; 3rd param
; rsi   ; 2nd param
; rdi   ; 1st param
; eax   ; syscall_number
; syscall

format ELF64 executable 3

segment readable executable

entry $

	mov	edx,msg_size	; CPU zero extends 32-bit operation to 64-bit
				; we can use less bytes than in case mov rdx,...
	mov	rsi, msg
	mov	edi,1		; STDOUT
	mov	eax,1		; sys_write
	syscall

	xor	edi,edi 	; exit code 0
	mov	eax,60		; sys_exit
	syscall

segment readable writeable

msg db 'Hello 64-bit world!',0xA
msg_size = $-msg
it demonstrates write() and exit() system calls

man 2 pipe tells you the argument pipe() takes is room for two file descriptors
a full example for 32bit linux:
Code:
format ELF executable 3
entry start

segment readable executable

start:

	;make pipe
	mov	ebx,fds ;pointer to a place where to store the resulting fd's
	mov	eax,42 ;sys_pipe
	int	0x80

	;send 'Hello world!',0xA to fd1
	mov	edx,msg_size
	mov	ecx,msg
	mov	ebx,[fd2]
	mov	eax,4 ;sys_write
	int	0x80

	;read msg from fd2
	mov	edx,256
	mov	ecx,tmp
	mov	ebx,[fd1]
	mov	eax,3 ;sys_read
	int	0x80
	mov	[pipe_msg_len], eax

	mov	edx,[pipe_msg_len]
	mov	ecx,tmp
	mov	ebx,1 ;stdout
	mov	eax,4 ;sys_write
	int	0x80

	mov	eax,1
	xor	ebx,ebx
	int	0x80

segment readable writeable

msg db 'Hello world!',0xA
msg_size = $-msg

fds:		;pointer to bout fds, fd's are 4byte each
fd1 rd 1	;place for fd1, read end of the pipe
fd2 rd 1	;place for fd2, write end of the pipe

tmp rb 256	;tmp piece of memory, 256 bytes

pipe_msg_len rd 1
as man 2 pipe says, you write to the second fd and that gets sent to the first fd (other way around does not work)


if something is not clear, ask
i'd advise to read the fasm manual first until you fully understand the hello world example
then to read the pipe() man page

edit: sorry, that you don't have a 64bit cpu went over my head
i'l make a 32bit example, just a sec
edit2: made it, hf
Thank you very much, Sir. Definitely the most helpful reply by far. I can allready see a couple errors I've been making.

Bill
 
Old 03-03-2015, 05:33 PM   #11
billvance
Member
 
Registered: Sep 2013
Location: Bothell, Washington USA
Distribution: Kubuntu 12.04
Posts: 60

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
SVC means 'Supervisor Call'; also known as 'system call' or 'syscall'. Meaning: the user program asks the kernel (or with another word: the executive) to do something.
Ah, the last two I'm most familiar with. 'Supervisor Call', I
don't see near as much. When it comes to abreviations and acronyms, I try to limit myself to those I find the most useful,
as some idiot is allways coming up with new ones by the bushel basket full, trying to sound cool for the boss or whatever.

Bill
 
Old 03-03-2015, 06:31 PM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
Also: nearly all the time, you do not want to write assembly-code directly ... except maybe within short asm { } blocks within a C/C++ program. You will not generate better object-code than an optimizing compiler would have done.
 
Old 03-03-2015, 07:45 PM   #13
genss
Member
 
Registered: Nov 2013
Posts: 744

Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
You will not generate better object-code than an optimizing compiler would have done.
i beg to differ
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Linux Assembly: write and receive assembly data errigour Programming 2 09-22-2012 09:54 AM
Pipes jayant17 Linux - Newbie 3 02-27-2010 03:04 PM
pipes leedude Programming 4 05-20-2008 06:52 PM
how to use pipes? rabbit2345 Linux - Software 2 03-29-2008 01:52 PM
about pipes kpachopoulos Programming 1 10-15-2005 12:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration