I am learning NASM and do not have any experience. I have decided to write an app that reads integer and floating point numbers from user input and prints them.
;;; File test.asm
;;;
;;; Required packages: nasm, gcc
;;; compile commands:
;;; nasm -f elf test.asm
;;; gcc -m32 -o test test.o
%define SYSCALL_EXIT 1 ; system call sys_exit
%define INT32_BYTES_SIZE 4
%define FLOAT32_BYTES_SIZE 4
section .data
questionInt32 db "Please enter any integer number.", 0xa, 0x0
questionFloat32 db "Please enter any decimal number.", 0xa, 0x0
prompt db "> ", 0x0
resultMessage db "Entered: int %d, float %e", 0xa, 0x0
scanAgeFmt db "%d", 0x0
scanFloatFmt db "%e", 0x0
section .bss
inum resb INT32_BYTES_SIZE
fnum resb FLOAT32_BYTES_SIZE
section .text
extern scanf
extern printf
global main
main:
call printInt32Prompt
call scanInt32
call printFloat32Prompt
call scanFloat32
call printMessage
mov eax, SYSCALL_EXIT
mov ebx, 0
int 0x80
ret
scanInt32:
push inum
push scanAgeFmt
call scanf
add esp, 8
ret
scanFloat32:
push fnum
push scanFloatFmt
call scanf
add esp, 8
ret
printMessage:
push DWORD [fnum]
push DWORD [inum]
push resultMessage
call printf
add esp, 12
ret
printInt32Prompt:
push questionInt32
call printf
add esp, 4
push prompt
call printf
add esp, 4
ret
printFloat32Prompt:
push questionFloat32
call printf
add esp, 4
push prompt
call printf
add esp, 4
ret
Reading of integer number works correctly, but I have a problem with floating point one. As you see below, entered value and value from output are different, and I do not have an idea what I have done wrong.
$ ./test
Please enter any integer number.
> 3
Please enter any decimal number.
> 3.4
Entered: int 3, float 8.104613e+107