From: Pea Jay <peajay@evobsyniva.com>
To: Stephan Sauerburger <stephan@sauerburger.org>
Cc: linux-assembly@vger.kernel.org
Subject: Re: math on variables
Date: Sun, 15 Dec 2002 16:20:37 -0500 [thread overview]
Message-ID: <20021215162037.5a942d0e.peajay@evobsyniva.com> (raw)
In-Reply-To: <20021215122906.GB8507@sauerburger.org>
> 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.
> 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)
> ;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.
> 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
prev parent reply other threads:[~2002-12-15 21:20 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-15 12:29 math on variables Stephan Sauerburger
2002-12-15 21:20 ` Pea Jay [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20021215162037.5a942d0e.peajay@evobsyniva.com \
--to=peajay@evobsyniva.com \
--cc=linux-assembly@vger.kernel.org \
--cc=stephan@sauerburger.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.