math on variables
Hi, I'm a newbie and trying to perform basic math on variables in memory (not the stack). I tried this but it gives lots of "error: invalid combination of opcode and operands" and "error: operation size not specified" messages:
section .bss
[...]
totalcoins resd 1
totalvalue resd 1
section .text
extern scanf
global main
main:
mov [totalcoins],0 ;This gives an error. I just want to
;initialize them to 0 at beginning.
mov totalvalue,dword 0 ;This doesn't work either,
;when I give its address with "name" ;in lieu of "[name]" or specify dword.
;;read in nickels
push dword nickels
push dword dformat
call scanf
add esp,8
;;add it to "totalcoins"
add [totalcoins],[nickels] ;error: invalid combination of
;opcode and operands. Tried ;"add totalcoins,dword [nickels]" too.
;add value in centes to "totalvalue"
;(nickels are worth 5 cents, so multiply by 5 first.)
mov eax,[nickels]
mul eax,5 ;error: invalid combination of
mov [nickels],eax ;opcode and operands.
I'm using nasm 0.98.28, on Debian Linux, on a Pentium III 450 mhz, 256 MB of ram. I find it odd, but I have found no information on such operations on variables anywhere on the web or my books. Perhaps I'm just dense. How can one accomplish these things? Thx.
Signature · 3 lines
-Stephan For PGP Public Key, visit: http://www.sauerburger.org
Quoted 3 lines
mov [totalcoins],0 ;This gives an error. I just want to
;initialize them to 0 at beginning.
NASM needs to know what size this variable is.
mov dword [totalcoins], 0
mov [totalcoins], dword 0
Since you're doing two variables, you might want to use a register to generate shorter code.
xor eax, eax
mov [totalcoins], eax
mov [totalvalue], eax
If you only run this function once, you can initalize them by putting them in the .data section.
Quoted 2 lines
add [totalcoins],[nickels] ;error: invalid combination of
;opcode and operands. Tried
You can only have one square-bracket thing per instruction, this is a limitation of the way the instructions are encoded. So you have to use two instructions.
You may want to forget zeroing totalcoins above, and just do this at the end:
mov eax, [quarters]
add eax, [dimes]
add eax, [nickels]
add eax, [pennies]
mov [totalcoins], eax
(and check for overflows on the adds if that is a posibility)
Quoted 5 lines
;add value in centes to "totalvalue" ;(nickels are worth 5 cents, so multiply by 5 first.)
mov eax,[nickels]
mul eax,5 ;error: invalid combination of
mov [nickels],eax ;opcode and operands.
Two things here. First is that the operands eax and edx are implied. The second is that the 5 must be either a register or a memory variable. So you should do one of these two:
mov eax, [nickels]
mov edx, 5
mul edx
mov [nickels], eax
...or, what I usually do with mul and div...
mov eax, [nickels]
mul [five] ; and somewhere have: five dd 5
mov [nickels], eax
...or, you could do this...
mov eax, 5
mul dword [nickels]
mov [nickels], eax
In all cases, eax and the specified memory or register are multiplied, and the result is stored in edx:eax. This is because a dword can hold up to 4294967295, and that value multiplied by itself is 18446744065119617025 which requires a quadword to store. Since you're not using quadwords, you need to check either edx and make sure it is zero, or before the multiplication make sure nickels is less than 858993459. If nickels won't be that large, then you can forget about it.
Quoted 2 lines
I find it odd, but I have found no information on such operations on variables anywhere on the web or my books. Perhaps I'm just dense.
I wouldn't be surprised if the information wasn't there. The hardest thing about assembly language is finding information. Once you get past that point it's pretty easy.
If you like, you can email me your questions in the future. I think this list is meant for linux specific questions.
- peajay