0

So, i faced with a problem, that print each value of variable on a new line, but format for print is a line. I really spend a lot of time that find solution. But this solution don't work. I think that is because i am noob in assembler. So, i deleted all my fail... This is my code:

global main
extern printf
section .data
str: db "%i", 10, 0
count: dw 1

section .text
main:
start:
push dword [count]
push dword str
call printf
push dword [count]
push dword str
call printf

So, now in terminal printed this:

1
1

What do I need to do to print like this:

1 1

Please, example me code, that i understand.... Please, don't send me first link in internet with similar problem, because i tried this. I really don't understand... How i can insert in print string with differents char, for example ':', 'space', '/' and other...? Thanks you :)

OKIS
  • 53
  • 1
  • 8
  • 2
    The `10` in the definition of `str` acts as an U+000A Line Feed, which is used as a linebreak. – ecm Feb 02 '21 at 19:26
  • @ecm I know. I tried delete it, but after this str does not print – OKIS Feb 02 '21 at 19:28
  • I use linux x86 (Virtual Machine) – OKIS Feb 02 '21 at 19:29
  • 1
    You probably need to flush the C library's buffers. – ecm Feb 02 '21 at 19:38
  • 1
    https://stackoverflow.com/questions/8502945/printf-without-newline-in-assembly?r=SearchResults – ecm Feb 02 '21 at 19:40
  • @ecm I tried this but nothing happens. If you can show me how to apply to my code, I will be very grateful – OKIS Feb 02 '21 at 19:51
  • @ecm I found the code on the Internet, adjusted it to my own, but it displayed unchanged, or nothing at all print. I don't understand how it works – OKIS Feb 02 '21 at 19:53
  • 1
    I updated some of the answers on the question @ecm linked; [Printf without newline in assembly](https://stackoverflow.com/a/8503215) has NASM code that works on Linux, so you can flush the buffers to see printf output even after removing the newline from the format string. – Peter Cordes Feb 02 '21 at 20:26

1 Answers1

1

Change

str: db "%i", 10, 0

to

str: db "%i ", 0

10 is a newline character. This replaces it with a space.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The file compiles but does not output anything. That is, if 10, 0, then displays ifnomation, if you remove 10, then nothing is displayed at all – OKIS Feb 02 '21 at 20:14
  • 1
    Make sure you call `exit()` at the end of the program so that the buffer will be flushed. – Barmar Feb 02 '21 at 20:15