Discussion:
Use of square brackets in 8086 memory addressing
(too old to reply)
cman
2007-03-10 13:54:55 UTC
Permalink
I am trying to understand memory addressing in 8086 assembler.
Especially use of the square brackets in the mov instruction. What
exactly does the square brackets refer to?

If I have a variable "str" and do the following, what does it mean:

mov str, "H" - is this legal code, what does it do?

and, mov [str], "H"

Also, I read that just using mov dx, str - would move the memory
address of "str" into dx, or is the the actual contents of "str" which
is Hello. I'm coming from C, looks like the [] is a dereference
operator, and the "address of" is implied in assembly. Please guide me
here.

tilak
Charles Crayne
2007-03-10 23:26:46 UTC
Permalink
On 10 Mar 2007 05:54:55 -0800
Post by cman
I'm coming from C, looks like the [] is a dereference
operator, and the "address of" is implied in assembly.
Not all assemblers use this convention, but there are several which do.
Which assembler are you most interested in?

-- Chuck
cr88192
2007-03-10 23:45:58 UTC
Permalink
Post by cman
I am trying to understand memory addressing in 8086 assembler.
Especially use of the square brackets in the mov instruction. What
exactly does the square brackets refer to?
mov str, "H" - is this legal code, what does it do?
not really.
most likely the assembler will reject this one.


note that in C, whenever a literal string is given, it is actually
implicitly stored somewhere else, and referenced where it is given.

assembler doesn't do this, so, for a string like "hello" you would do
something like:
str db "hello", 0

and when using it:
mov ax, str
push word str
....
Post by cman
and, mov [str], "H"
if this works at all (depends on the assembler I guess), likely this is not
doing what you may be expecting it to do.

H here will be interpreted as an integer, and stored in str, like an
integer.
Post by cman
Also, I read that just using mov dx, str - would move the memory
address of "str" into dx, or is the the actual contents of "str" which
is Hello. I'm coming from C, looks like the [] is a dereference
operator, and the "address of" is implied in assembly. Please guide me
here.
in this case, it will move the address of str into dx.

mov dx, [str]
would move the first 2 chars of str into dx.


'str' generally refers to the address of str.

'[str]'
basically means that it is accessing the value of str, rather than the
address of str.


note that assembler is a lot closer to what goes on in the HW, wheras C is
more of an abstraction, and assembler does not do all the various nice and
tidy tricks that C does.


so, '[val]' is closer to what would be in C, 'val', and 'val' is closer to
what would be in C, '&val' (sometimes, in C this magic depends on type,
where the default type for strings is either implicitly, or explicitly, a
pointer).

it can be viewed that all variables in assember are in '&' form, and in this
way brackets do work for deference, ei: '[val]' meaning '(&val)[0]', or more
simply, 'val'.

which extends nicely (some cases): '[val+5]' being '(&val)[5]' (assuming
'val' is 'char', note that C implicitly multiplies indices by
'sizeof(type)').

for strings '[str+5]' is the same as 'str[5]'.


so, yes, the key here is pointers.
nearly everything in assembler is pointers (or integers), and 'value' is
itself a rather fleeting concept.


dunno if any of this helps at all.
Post by cman
tilak
.
Steven Nichols
2007-03-12 20:35:15 UTC
Permalink
Post by cman
I am trying to understand memory addressing in 8086 assembler.
Especially use of the square brackets in the mov instruction. What
exactly does the square brackets refer to?
It depends on the assembler. In NASM, the square brackets always load or
store to the address represented by the expression within the square
brackets. Some assemblers don't always use brackets when the label is
defined as a varible. Generally the square brackets mean an absolute
register load or store from/to the address in brackets.
Steve

www.ml1compiler.org

Loading...