0

I have this unfinished assignment where I need to convert a negative float read from the keyboard without scanf and calculate its abs.

extern printf

section .data
    numar dq 0.0
    formatare db "%f"

section .bss
    buffer resb 1
    zecimi resb 8
    negativ resb 1

section .text

global _start

_start:
    mov eax, 10
    jmp loop
    
loop:
    mov rax, 0
    mov rdi, 0
    mov rsi, buffer
    mov rdx, 1
    syscall
        
    cmp byte [buffer], 'A'
    je end_loop

    cmp byte [buffer], '-'
    je set_negativ 
        

    mov rdx, qword [numar] 
    mul rdx 
    sub byte [buffer], '0'
    add rdx, buffer
    mov qword [numar], rdx
        
    jmp loop
    
end_loop:
    movsd xmm0, qword [numar]     
    mov  rdi, formatare           
    mov  eax, 1               
    call printf
    mov rax, 1
    mov rdi, 0
    syscall

set_negativ:
    mov byte [negativ], 1
    jmp loop

;modul:
;    fldl [numar]
;    fabs
;    fstpl [numar]

I wanted to compile it and print the number to at least check if the reading part works.

I ran nasm -f elf64 program.asm and ld -m elf_x86_64 -lc program.o with no errors, creating a 14kb ish a.out file. The file is visible in ls, it is visible in my file explorer, but running it does this:

$ ./a.out 
bash: ./a.out: No such file or directory

The output of file is this:

$ file a.out 
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld64.so.1, not stripped

I did give it permissions to execute with sudo chmod +x * just to be safe and it still does not work. I am probably missing out something obvious but I could not find anything similar on the internet.

I expected to be able to run the program and see whether the code I wrote is valid or not so I could move on with the solution.

Oreoezi
  • 129
  • 9
  • 1
    Another bug you're going to discover is that `printf` buffers its output, and exiting via syscall won't flush those buffers. You would need to exit by calling `exit()` instead. As a general rule, if you're going to use libc at all, it is safest to write your asm to mimic a C program: use `main` as your entry point instead of `_start`, call standard library functions instead of raw system calls, and exit by calling `exit()` or returning from `main`. – Nate Eldredge May 06 '23 at 21:32
  • 1
    Smells like wrong dynamic linker to me. Try to link through the C compiler: `cc -nostartfiles program.o` – fuz May 06 '23 at 23:50
  • Thank you both for the valuable input. In the end I did `gcc -m64 -no-pie -nostartfiles program.o` It's a shame that I have yet to figure it out how to do the same with `ld`, but I will post a comment if I do – Oreoezi May 07 '23 at 15:34

0 Answers0