Printf without a new line in the assembly

I recently read this article about using printf and scanf in an assembly:

Intfmt value: db "% d", 10, 0 in assembly

In particular, it says "In printf, a new line prints a new line, and then (if the output is in buffered mode in the line, which probably is), it clears the internal output buffer, so you can see the result. Therefore, when you delete 10 and you donโ€™t see the conclusion. "

However, I do not know what to do if I do not need a new line after my output in my assembly file. Here is a simple test file that I wrote for printing without a new line:

extern printf LINUX equ 80H ; interupt number for entering Linux kernel EXIT equ 60 ; Linux system call 1 ie exit () section .data int_output_format: db "%ld", 0 segment .text global main main: mov r8, 10 push rdi push rsi push r10 push r9 mov rsi, r8 mov rdi, int_output_format xor rax, rax call printf pop r9 pop r10 pop rsi pop rdi call os_return ; return to operating system os_return: mov rax, EXIT ; Linux system call 1 ie exit () mov rdi, 0 ; Error code 0 ie no errors syscall ; Interrupt Linux kernel 64-bit 

but as a read article assumes stdout is not cleared. I was thinking, maybe I need to somehow wash off after the number is displayed? But I'm really not sure.

I am using NASM assembly language.

Thanks in advance!

+6
source share
4 answers

The correct answer to my question is as suggested by Bazile Starinkevich in the commentary above. I needed to add to my code:

 extern fflush ... xor rax, rax call fflush ... 
+3
source

Call fflush(stdout); to display what is currently in the buffers.

+3
source

In FASM

 push [_iob] call [fflush] 

For NASM Users

 extern fflush extern stdout ... push dword [stdout] call fflush add esp, 4 etc... 
+3
source

Another option is to remove the standard stdout stream line buffering. Here's a C call for this. Translation to assembly allows as an exercise, since I donโ€™t think it makes sense to do file / stream input / output operations in ASM, the cost / benefit is extremely wrong.

 setvbuf(stdout, NULL, _IONBF, 0); 

This way each printf (and fputs , putc , puts , etc.) will have an implicit fflush

+1
source

Source: https://habr.com/ru/post/903750/


All Articles