RicardasSim icon

c printf from asm

RicardasSim | PRO | 09/04/19 06:04:41 PM UTC | 0 ⭐ | 13002 👁️ | Never ⏰ | []
ASM (NASM) |

0 B

|

None

|

0 👍

/

0 👎

;trying to print from nasm asm with c printf, Ubuntu 18.04 64bit
;examples: https://www.csee.umbc.edu/portal/help/nasm/sample_64.shtml
;64 bit
;nasm -f elf64 main.asm -o main.o
;gcc -no-pie -m64 -o asm64_printf main.o
;./asm64_printf
 
extern printf
extern strlen
 
SECTION .bss
        
    floatTmp: resq 1    
 
section .data
 
    ;null terminated string
    msg_1: db "Hello !", 0
    format_1: db "%s", 10, 0
 
    ;null terminated string
    msg_2: db "Hello again !", 0
    msgLen_2 equ $-msg_2
    format_2: db "%s length (with null terminator): %d", 10, 0
 
    ;string strlen
    format_3: db "%s string lenght: %zu", 10, 0
 
    ;char
    char_1 db 'c'
    format_4: db "%c", 10, 0
    format_5: db "%c %c %c", 10, 0
 
    ;integer
    int_1 dd 1234567890
    format_6: db "integer: %d", 10, 0
 
    ;long integer
    lInt_1 dq 1234567890123456789
    format_7: db "long integer: %ld", 10, 0
 
    ;hex
    hex_1: dd 0x12345
    format_8: db "hex: %X", 10, 0
    format_9: db "hex: %08X", 10, 0
 
    ;long hex
    hexL_1: dq 0x123456789
    format_10: db "hex (long): %lX", 10, 0
    format_11: db "hex (long): %016lX", 10, 0
 
    ;float
    float_1: dd 12.345
    format_12: db "float: %f", 10, 0
 
    ;double
    double_1: dq 12345.1234567
    format_13: db "double: %f", 10, 0
 
    format_14: db "float: %f double: %f", 10, 0
    format_15: db "char: %c float: %f double: %f", 10, 0
    format_16: db "char: %c integer: %d float: %f double: %f", 10, 0
 
    ;TODO long double
    ldouble_1: dt 1234567.1234567
    format_17: db "long double: %Lf", 10, 0
    format_18: db "long double: %.7Lf", 10, 0
 
section .text
global main
 
main:
 
    push    rbp
 
    ;string
    mov rdi,format_1
    mov rsi,msg_1
    xor rax,rax ;mov rax,0
    call printf
 
    ;string, length
    mov rdi,format_2
    mov rsi,msg_2
    mov rdx,msgLen_2
    xor rax,rax ;mov rax,0
    call printf
 
    ;string strlen
    mov rdi,msg_2
    call strlen
 
    mov rdi,format_3
    mov rsi,msg_2
    mov rdx,rax ;strlen result
    xor rax,rax ;mov rax,0
    call printf
 
    ;char
    mov rdi,format_4
    mov rsi,[char_1]
    xor rax,rax ;mov rax,0
    call printf
 
    ;char char char
    mov rdi,format_5
    mov rsi,[char_1]
    mov rdx,[char_1]
    mov rcx,[char_1]
    xor rax,rax ;mov rax,0
    call printf 
 
    ;integer
    mov rdi,format_6
    mov rsi,[int_1]
    xor rax,rax ;mov rax,0
    call printf
 
    ;long integer
    mov rdi,format_7
    mov rsi,[lInt_1]
    xor rax,rax ;mov rax,0
    call printf
 
    ;hex
    mov rdi,format_8
    mov rsi,[hex_1]
    xor rax,rax ;mov rax,0
    call printf 
 
    mov rdi,format_9
    mov rsi,[hex_1]
    xor rax,rax ;mov rax,0
    call printf 
 
    ;long hex
    mov rdi,format_10
    mov rsi,[hexL_1]
    xor rax,rax ;mov rax,0
    call printf 
 
    mov rdi,format_11
    mov rsi,[hexL_1]
    xor rax,rax ;mov rax,0
    call printf     
 
    ;float
    fld dword [float_1]
    fstp qword [floatTmp]
 
    mov rdi,format_12
    movq xmm0, qword [floatTmp]
    mov rax, 1
    call printf 
 
    ;double
    mov rdi,format_13
    movq xmm0, qword [double_1]
    mov rax, 1
    call printf
 
    ;float and double
    mov rdi,format_14
    movq xmm0, qword [floatTmp]
    movq xmm1, qword [double_1]
    mov rax, 2
    call printf     
 
    ;char, float, double
    mov rdi,format_15
    mov rsi,[char_1]
    movq xmm0, qword [floatTmp]
    movq xmm1, qword [double_1]
    mov rax, 2
    call printf 
 
    ;char, int, float, double
    mov rdi,format_16
    mov rsi,[char_1]
    mov rdx,[int_1]
    movq xmm0, qword [floatTmp]
    movq xmm1, qword [double_1]
    mov rax, 2
    call printf 
 
    ;TODO long double    
    mov rbp, rsp
    sub rsp, 16
    mov rdi,format_17
    fld tword [ldouble_1]
    fstp tword [rbp-10h]
    push qword [rbp-8h]
    push qword [rbp-10h]
    xor rax,rax ;mov rax,0
    call printf
    ;add rsp, 16
    mov rsp,rbp
 
    ;mov rbp, rsp
    sub rsp, 16
    mov rdi,format_18
    fld tword [ldouble_1]
    fstp tword [rbp-10h]
    push qword [rbp-8h]
    push qword [rbp-10h]
    xor rax,rax ;mov rax,0
    call printf
    ;add rsp, 16
    mov rsp,rbp
 
    pop rbp
 
    xor rax,rax ;mov rax,0
    ret
 
;output:
;
;Hello !
;Hello again ! length (with null terminator): 14
;Hello again ! string lenght: 13
;c
;c c c
;integer: 1234567890
;long integer: 1234567890123456789
;hex: 12345
;hex: 00012345
;hex (long): 123456789
;hex (long): 0000000123456789
;float: 12.345000
;double: 12345.123457
;float: 12.345000 double: 12345.123457
;char: c float: 12.345000 double: 12345.123457
;char: c integer: 1234567890 float: 12.345000 double: 12345.123457
;long double: 1234567.123457
;long double: 1234567.1234567
;

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎