0

code:

extern printf
extern scanf
extern puts

global main
section .rodata
    msg1    db          "Enter positive Number:",0
    arg1    db          "%s",0
    arg2    db          "%u",0
section .text
main:
    push    rbp
    mov     rbp     ,   rsp
    sub     rsp     ,   8           ; what is purpose of this stack allign

    xor     rax     ,   rax         ; no floating point 
    mov     rdi     ,   arg1
    mov     rsi     ,   msg1
    call    printf

    ;How can i call from scanf using stack

    add     rsp     ,   8
    pop     rbp
    ret

I tried something like

1)

xor     rax     ,   rax
mov     rdi     ,   arg2
lea     rsi     ,   [rsp+4]     ;first four byte
call    scanf

2)

xor     rax     ,   rax
mov     rdi     ,   arg2
mov     rsi     ,   rsp         ;last four byte
call    scanf

3)

sub     rsp     ,   4

xor     rax     ,   rax
mov     rdi     ,   arg2
mov     rsi     ,   rsp         ;newely allocated 4 byte
call    scanf

add     rsp     ,   4

4)

xor     rax     ,   rax
mov     rdi     ,   arg2
lea     rsi     ,   [rsp-4]     ;newely allocated 4 byte
call    scanf

This all are throws Segmentation fault (core dumped)

1)What is the perfect way to call scanf or printf from assembly

I am using nasm(assembler) and gcc(linker) with kali linux subsystem(wsl) in windows x86-64(64 bit)

Thanks.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
  • `push rbp` already re-aligns the stack, then you mis-align. You need an odd multiple of 8 after function entry, because `call` pushed an 8 byte return address. So `sub rsp, 16` would work. – Peter Cordes Apr 26 '20 at 05:31
  • 4) `lea rsi, [rsp-4]` - that stack space below the current RSP overlaps with where `call` is going to push a return address. So even if RSP was correctly aligned, you'd have gotten scanf to partially overwrite its own return address. – Peter Cordes Apr 26 '20 at 05:37
  • Unfortunately most of the duplicates I looked for use static storage for the destination of scanf. Allocating on the stack is totally fine; you just need the stack aligned properly when the `call` instruction executes. – Peter Cordes Apr 26 '20 at 05:42
  • I changed sub rsp ,16 and add rsp ,16 8 to 16 and using 2) form to input but still seg fault presents – srilakshmikanthanp Apr 26 '20 at 05:49
  • Which instruction segfaults? Is it still inside scanf, or is it in your code now? – Peter Cordes Apr 26 '20 at 05:57
  • when i enter number and press enter next this shows seg fault – srilakshmikanthanp Apr 26 '20 at 06:00
  • Works for me with your code copy/pasted into a file, with version 2). `nasm -felf64 scan.asm && gcc -no-pie scan.o` makes a working executable on my system (Arch GNU/Linux, not under WSL). Trying to link without `-no-pie` fails because Arch configures GCC with `-pie` as the default. The exact source I used was https://godbolt.org/z/voqAaZ – Peter Cordes Apr 26 '20 at 06:00
  • stack pointer + 8 should be kept 16 byte aligned upon entry to functions. This means when call any function in main the stack must be aligned in multiple of 16 ? – srilakshmikanthanp Apr 26 '20 at 06:03
  • Right. Aligned before a call, (including the call *to* main), and call itself pushes an 8 byte return address. If it still doesn't work for you, run it under GDB so you can see which instruction faults. Debugging asm without a debugger is a waste of your time; do yourself a favour and learn to use a debugger to single-step and look at registers / memory. (Some tips at the bottom of https://stackoverflow.com/tags/x86/info) – Peter Cordes Apr 26 '20 at 06:04
  • The code works thanks a lot? But why the stack is aligned in multiple of 16 under main ? – srilakshmikanthanp Apr 26 '20 at 06:10
  • and before main the does the stack aligned in multiple of 16 and what is the difference between main: and _start: both are behave same way or not? – srilakshmikanthanp Apr 26 '20 at 06:13
  • `_start` is not a function, it's not called by anything. The ABI guarantees that the stack is aligned by 16 on entry to `_start` (from the kernel). As for why, [Why does System V / AMD64 ABI mandate a 16 byte stack alignment?](https://stackoverflow.com/q/49391001) – Peter Cordes Apr 26 '20 at 06:16
  • Don't we have to clear `al` for no xmms when calling varargs functions? – Erik Eidt Apr 26 '20 at 15:10
  • what did you mean i can't understand clearly, I cleared al with xor rax ,rax – srilakshmikanthanp Apr 26 '20 at 15:18

0 Answers0