LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-07-2019, 03:49 AM   #1
Sunless
LQ Newbie
 
Registered: Dec 2019
Posts: 17

Rep: Reputation: Disabled
sys_write doesn't print a message to the terminal


Hi guys!

I'm trying to explore GNU Assembler using this example: https://0xax.github.io/asm_1/

My code of main.s is
Quote:
.data
message: .ascii "ASSEMBLY OUTPUT"

.text
.globl _start

_start:
movq $1, %rdi
movq $1, %rax
movq $15, %rdx
movq $message, %rsi
syscall
movq $60, %rax
movq $0, %rdi
syscall
Command line preparation:

Quote:
as -g -o main.o main.s

ld -o main main.o
Now, I run ./main But it doesn't print anything and terminal is waiting my next command.

What's going wrong?

In GNU Debugger numbers were successfully stored in registers.

I see result of first syscall "-14" in RAX.
 
Old 12-07-2019, 05:16 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It does work for me (linux/amd64). The only thing I miss is a line-feed:
Code:
$ as -g -o hello_linux.o hello_linux.s
$ ld -g -o hello_linux hello_linux.o
$ ./hello_linux
ASSEMBLY OUTPUT$

Last edited by NevemTeve; 12-07-2019 at 06:10 AM.
 
1 members found this post helpful.
Old 12-07-2019, 05:39 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Your code works here.
Code:
$ cat hello.s
.data
message: .ascii "ASSEMBLY OUTPUT"

.text
.globl _start

_start:
movq $1, %rdi
movq $1, %rax
movq $15, %rdx
movq $message, %rsi
syscall
movq $60, %rax
movq $0, %rdi
syscall 
$ as -o hello hello.s
$ ld -o hello hello.o
$ ./hello 
ASSEMBLY OUTPUT$
errno 14 is EFAULT "bad address", which suggests it can't read the message text.

I wrote a similar example here https://www.linuxquestions.org/quest...5/#post6020662
Maybe see if that gives the same error.
 
1 members found this post helpful.
Old 12-07-2019, 05:53 AM   #4
Sunless
LQ Newbie
 
Registered: Dec 2019
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thank you. I didn't saw result message in terminal because there is no '\n' in the message text. Program works. With .string I have 18 in RAX result (do you mean it?). Where did you found the error code description?
 
Old 12-07-2019, 05:59 AM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
I see result of first syscall "-14" in RAX.
error codes are negative of errno. Positive in %rax means it worked, returning the number of characters written.

The errno values are (eventually - includes that include includes, that include includes...) in:
/usr/include/asm-generic/errno-base.h
/usr/include/asm-generic/errno.h

man 2 write, gives explanation of the various returns that sys_write may give.

Last edited by GazL; 12-07-2019 at 06:11 AM.
 
2 members found this post helpful.
Old 12-07-2019, 07:15 AM   #6
Sunless
LQ Newbie
 
Registered: Dec 2019
Posts: 17

Original Poster
Rep: Reputation: Disabled
Could I optimize my program with more code using these compilation instructions on my corei7?
Quote:
as -mtune=corei7 -o main.o main.s
ld -Ofast -o main main.o
Does the linker work with Ofast like GCC? There is no -Ofast in ld -help.
 
Old 12-07-2019, 09:56 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by Sunless View Post
Thank you. I didn't saw result message in terminal because there is no '\n' in the message text.
I spotted this immediately. Standard output is buffered, and typically only flushed when you output a line feed. The writes (EDITING TO CLARIFY: as in the actual display of characters on the screen) happen during flushes.

Here are some explanatory hits I got from google:

https://eklitzke.org/stdout-buffering
https://www.turnkeylinux.org/blog/unix-buffering
http://www.pixelbeat.org/programming/stdio_buffering/
https://c-for-dummies.com/blog/?p=3701

Last edited by dugan; 12-08-2019 at 01:39 PM.
 
1 members found this post helpful.
Old 12-08-2019, 06:48 AM   #8
Sunless
LQ Newbie
 
Registered: Dec 2019
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thank you. I have found these pages more clear to me:
https://www.computerhope.com/jargon/...descriptor.htm
https://www.computerhope.com/jargon/s/stdout.htm
https://en.wikipedia.org/wiki/Control_character

Is there something lightweight than fsync syscall?
Code:
 .data
    message: .ascii "ASSEMBLY OUTPUT MESSAGE"
    .equ one, 1
    len = . - message

     .text
    .globl _start

    _start:
    movq $one, %rdi
    movq $one, %rax
    movq $len, %rdx
    movq $message, %rsi
    syscall #sys_write
    movq $74, %rax #rdi == 1
    syscall #fsync
    movq $60, %rax
    xor %rdi, %rdi
    syscall #exit
github syscall reference

Where could I find official reference to control characters influence on stdout and stderr? Did that limited by bash in a terminal?
 
Old 12-08-2019, 07:33 AM   #9
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
libc's stdout FILE stream is certainly buffered, so you might need to fflush(stdout) in some C programs, but for a asm program making a direct call of the write(2) syscall writing to the tty on FD 1, I believe the write is unbuffered and therefore you don't need to flush/fsync. (someone please correct me if I'm wrong here).

You usually only use fsync() when you need to be sure something has gotten to disk before continuing, such as writing data to disk files, and even then you normally only do it when your program needs to periodically checkpoint or when you're done with the file and are about to close the FD.
 
1 members found this post helpful.
Old 12-16-2019, 01:02 PM   #10
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
On a somewhat sidenotey sidenote:

If you are learning assembler just for fun, then maybe do it for old computers, like the c64, spectrum and co.
Why? Because, at least in my opinion, at this current point in time it is incredibly unlikely for you to do things faster than the established compilers for C, C++ etc when it comes to the PC.

This is also true for the C64 et al, yes, people have been hacking these things for 40+ years now, but you get to play with awesome things like sid chips, vic chips, actual limitations to gauge your skill in making things lightweight against, and you also have more or less a faster product cycle.

I wish I could have posted this without bumping. I could have private messaged you, yes, but I also think that assembler is really not very much needed these days, as in, highly diminishing returns, and I think that anyone should be at least somewhat aware of this.
Even arduinos and co are mostly programmed with C, etc, these days.

My post is not intended to rain on your parade or anything, but, on the offchance that you're just chasing that assembler feeling, why not go with something that has, at least, again, in my opinion, a more fun yield.

Plus, it can still be lucrative! The "8bit-guy" made a C64 game recently and I think it sold rather well. I doubt a program, or game written in assembler has that kind of marketability on the PC these days.

Last edited by Geist; 12-16-2019 at 01:13 PM.
 
  


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
how did a system call invoke kernel call,ie: fwrite->sys_write jinxin16897123 Linux - Kernel 1 04-05-2012 02:04 PM
How to print a message from kernel module to terminal console or tty console yogesh_bansal Linux - Newbie 2 02-27-2009 09:10 AM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 04:15 PM
problem in intercepting sys_write system call appas Programming 7 09-03-2004 12:33 PM
calling sys_read inside sys_write returns number of bytes read as zero appas Linux - Software 0 08-28-2004 07:21 AM

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

All times are GMT -5. The time now is 06:48 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