ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
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.
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)
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
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).
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 Frantiek 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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.