I just started out today with assembly, so I wanted to build a simple program which takes 2 numbers and prints their result.
Since I'm using mac, I have to assemble my code in 64 bits, so I assembled it using nasm -f macho64 -l calc.lst cal.asm, when I got the following errors:
calc.asm:25: error: Mach-O 64-bit format does not support 32-bit absolute addresses
calc.asm:33: error: Mach-O 64-bit format does not support 32-bit absolute addresses
enter code here
Here are lines 25 and 33:
[25] mov rbx, [input] ; Store the value of input
...
[33] add rbx, [input] ; Add the value of the latest input to the previous one
All I'm doing is referencing a pointer...
I tried adding qword before the opening square bracket, but it changed nothing.
(and I know I am not doing the arithmetic correctly since I'm adding the ASCII values but I'll fix it later)
Here is the full code:
global start
section .data
newline: db 10
input: times 256 db 0
.len: equ $ - input
prompt1: db 'Enter a number: '
.len: equ $ - prompt1 ; The difference between the current working address and prompt1's address: the length of prompt1
prompt2: db 'Enter another number: '
.len: equ $ - prompt2
resultmsg: db 'The result is: '
.len: equ $ - resultmsg
section .text
start:
mov rsi, prompt1 ; Address of message
mov rdx, prompt1.len ; Length of message
call print
call read
call print_newline
mov rbx, [input] ; Store the value of input
mov rsi, prompt2
mov rdx, prompt2.len
call print
call read
call print_newline
add rbx, [input] ; Add the value of the latest input to the previous one
mov rsi, resultmsg
mov rdx, resultmsg.len
call print
push rbx ; Push the sum in order to get a pointer to it (in rsp)
mov rsi, rsp
mov rdx, 8
call print
call print_newline
call exit
print:
mov rax, 0x2000004 ; Set command to write string
mov rdi, 1 ; Set output to STDOUT
syscall ; Call kernel
ret
print_newline:
mov rsi, newline
mov rdx, 1
call print
ret
read:
mov rax, 0x2000003 ; Set command to read string
mov rdi, 0 ; Set input to STDIN
mov rsi, input
mov rdx, input.len
syscall
ret
exit:
mov rax, 0x2000001 ; Set command to exit
mov rdi, 0 ; Set exit code to 0 (normal execution)
syscall