Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using printf from c in Nasm causes the string to add random end line statment

this morning i tried to make a simple output library for my asm projects, i realized using the sys call each time is a waste of time and so i decided to automatize the program using printf function from c, so i tried to make a function with a little help from git hub forums and a bit of ai explaining me how asm works and i came up with this code.

SECTION .data
    endLine db 0Ah, 0
    integer_printf:     db  '%d',   10,0
    float_printf        db  '%f',   10,0
    string_format       db  '%s',   10,0

SECTION .text
    extern printf

printStr:
    ; mov rsi, string please put rsi to have the same value as your string variable (pointer)
    mov rdi,string_format ; mov rdi,printf_format
    call printf
    ret

%include '../script.asm'
%include '../funcio.asm'

extern printf

global main

section .data
    float_point     dq     70.232
    integer_value   dq     33      
    string_value    db     "Hello world!", 0

section .text

main:
    movsd xmm0, [float_point]
    call printFloat
    mov rsi, [integer_value]
    call printInt

    mov rsi, string_value
    call printStr
    call printStr
    call printStr
    call printStr
    call printStr

    mov rdi, 0
    call exit_program

This code is subdivided in two files and for some reason when i print the string hello world it will increase the number of new lines each time a new print function is called.
those are the infos:
expected output:

33
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!

obtained output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

70.232000
33
Hello world!
Hello world!

Hello world!


Hello world!



Hello world!

thanks for any help in advice!

>Solution :

RSI and RDI are caller-saved in most calling conventions — which means the callee can use them at will without worrying about saving them. For the same reason you don’t have to push them before using them and pop them after, other functions don’t have to either, so you can’t rely on them to retain their values.

You’ll need to reload them each time.

    ...
    mov rsi, string_value
    call printStr
    mov rsi, string_value
    call printStr
    mov rsi, string_value
    call printStr
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading