ChatGPT and NASM
section .bss
num resb 10 ; Reserve 10 bytes for input (enough for an integer)
section .data
prompt db 'Enter an integer: ', 0 ; The prompt message
prompt_len equ $-prompt ; Length of the prompt message
section .text
global _start
_start:
; Print the prompt message
mov rax, 1 ; Syscall number for sys_write
mov rdi, 1 ; File descriptor (stdout)
mov rsi, prompt ; Pointer to the prompt message
mov rdx, prompt_len ; Length of the prompt message
syscall
; Read the input (integer as a string)
mov rax, 0 ; Syscall number for sys_read
mov rdi, 0 ; File descriptor (stdin)
mov rsi, num ; Pointer to buffer for input
mov rdx, 10 ; Max number of bytes to read
syscall
; Print the input back to the screen (echo it)
mov rax, 1 ; Syscall number for sys_write
mov rdi, 1 ; File descriptor (stdout)
mov rsi, num ; Pointer to the input buffer
mov rdx, 10 ; Max number of bytes to write (we could improve this by calculating the string length)
syscall
; Exit the program
mov rax, 60 ; Syscall number for sys_exit
xor rdi, rdi ; Return code 0
syscall