I have decided to learn assembler for fun. I have been coding in C for many years.
I followed some online tutorials that print “Hello world” and dug around a bit in the NASM manual. All well and good. So, I set myself a task of printing “hello world” in a loop. I know I can do this with the loop opcode, but wanted to code it explicitly and use variables defined in the .bss section.
However, I obviously misunderstand how variable assignment works in assembly as I get the error message:
nasm -felf -o hello.o hello.asm
hello.asm:16: error: invalid combination of opcode and operands
hello.asm:17: error: invalid combination of opcode and operands
hello.asm:28: error: invalid combination of opcode and operands
I have tried searching the web for info on variable assignment, including the NASM manual, but can’t seem to find the information I need. Can anyone assist? Here’s my (simple!) code:
; print "Hello world!" to the screen multiple times
section .data
msg: db 'Hello world!', 10
msglen: equ $ - msg
section .bss
iter: resb 1
section .text
global _start
_start:
; loop 10 times
mov iter, 0 ; initalise loop counter
FL: cmp iter, 10 ; is iter == 10?
jge LoopEnd
; write the message to STDOUT:
mov eax,4 ; code for write syscall
mov ebx,1 ; stdout fd
mov ecx,msg ; message to print...
mov edx,msglen ; ...and it's length
int 80h ; kernel interrupt
; increment loop iterator
inc iter
jp FL
LoopEnd:
; now exit, with return code 0:
mov eax,1
mov ebx,0
int 80h
To make a memory reference in nasm, you must surround the address with square brackets. Additionally, in each of the cases you’ve got here, you also need to specify a size, like so:
In this case, though, it would probably make more sense to store
iterin a register instead of in memory. You’re clobbering most of the obvious ones with your system calls, butesioredilook available.