Assembly : add two numbers
Pergunta
I would like to add two number num1B and num2B and store the number in result, finaly show result.
But, when i launch nasm, it says :
prog2_1.txt:4: warning: attempt to initialize memory in a nobits section: ignored
prog2_1.txt:5: warning: attempt to initialize memory in a nobits section: ignored
prog2_1.txt:6: warning: attempt to initialize memory in a nobits section: ignored
my code :
org 0x0100 ;
section .bss
num1B: db 0Ah ; init num1B to 0Ah
num2B: db 00111111b ; init num2B to 00111111b
result: db 0 ; init result to 0
section .data
section .text
mov AX,0 ; AX = 0
add AX,[num1B] ; AX = AX + num1B
add AX,[num2B] ; AX = AX + num2B
mov [result],AX ; result = result + AX
mov DX,[result] ; show result
mov AH,09h
int 21h
mov AH,4Ch
int 21h
Thank you
Solução
You need to change your .bss
section to .data
section. The .bss
section is meant for uninitialized data, while the .data
section is meant for initialized data. That's why you can't use db
, dw
and so forth in .bss
section. Instead, you can place them in .data
section. Similarly, you can use resb
. resw
and so forth in .bss
section but not in .data
section.
In short, .data
is for initialized data and .bss
is for uninitialized data.
Outras dicas
You're right, it's a long road. If you want "easy", stick to BASIC! You're making progress. You've got your two numbers in ".data", not ".bss", but I assume they're still "db". When you use ax for the addition, it uses 16 bits - a "word" or two bytes. This puts one number in al and the second number in ah - not what you want! Either use al for the addition, or make your two numbers (and the result!) "dw" rather than "db". "Display the result" is the hard part, and doing words is probably easier than bytes, besides letting you display numbers bigger than 255. Someone has just posted a "display the result" routine... as a macro for Masm, but you should be able to modify the code to work in Nasm. You probably don't want a macro - it'll duplicate the same code every time you use it. A subroutine would be better, but if you don't know how to do that just put it "in line" - you're only doing it once (for now). If you continue to have trouble, post some more recent code...
Best, Frank http://www.nasm.us