The MASM Forum

General => The Campus => Topic started by: masterori on March 09, 2016, 04:59:19 PM

Title: Need help figuring out calculation of addresses of variables
Post by: masterori on March 09, 2016, 04:59:19 PM
So either I'm bad at math or just not understanding how to correctly calculate the memory addresses of a variable.

E.g I got this question right (at least I think so - if not, then I probably picked the answer closest to what I got).

The following data segment starts at memory address 0x1700 (hexadecimal)
.data
printString BYTE "Assembly is fun",0
moreBytes BYTE 19 DUP(0)
dateIssued DWORD ?
dueDate DWORD ?
elapsedTime WORD ?

What is the hexadecimal address of dueDate?
Answer: 0x1727

This one I got wrong

The following data segment starts at memory address 0x1000 (hexadecimal)
.data
printString BYTE "Do not add decimal to hex",0
someBytes WORD 27 DUP(0)
moreBytes BYTE 10, 20, 30, 40, 50, 60, 70, 80, 90
questionAddr DWORD ?
ignoreMe WORD ?

What is the hexadecimal address of questionAddr?


For sure, whatever calculation I come up with, it's either not there or is there but wrong.
Am I suppose to be counting the null terminating byte as part of the length?
Title: Re: Need help figuring out calculation of addresses of variables
Post by: mabdelouahab on March 09, 2016, 05:58:12 PM
addr questionAddr =start addr+sizeof printString +sizeof someBytes +sizeof moreBytes  = start addr +59h
Title: Re: Need help figuring out calculation of addresses of variables
Post by: jj2007 on March 09, 2016, 06:55:30 PM
Quote from: masterori on March 09, 2016, 04:59:19 PMAm I suppose to be counting the null terminating byte as part of the length?

Not as part of the length ("string" is 5 bytes long), but as part of size: db "string", 0 occupies 6 bytes in memory.

someBytes WORD 27 DUP(0) ->how many bytes? The name of this variable is misleading ;)
Title: Re: Need help figuring out calculation of addresses of variables
Post by: masterori on March 15, 2016, 04:38:30 PM
Quote from: mabdelouahab on March 09, 2016, 05:58:12 PM
addr questionAddr =start addr+sizeof printString +sizeof someBytes +sizeof moreBytes  = start addr +59h
So I did that for this problem and got 04B0

.data
list dword 50 dup(?)
matrix dword 20 dup(5 dup(?))
...

Hexadecimal base address of "matrix" is 0x04B0 is what I got since matrixAddr = startAddr + sizeof list = 1000h + 200h = 04B0. But the answer is 10C8. Can you point out what I did wrong?
Title: Re: Need help figuring out calculation of addresses of variables
Post by: jj2007 on March 15, 2016, 05:01:09 PM
Why 200h?

include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Inkey "The answer is ", Hex$(1000h+200)
EndOfCode
Title: Re: Need help figuring out calculation of addresses of variables
Post by: masterori on March 15, 2016, 05:17:43 PM
So I'm a bit confused here, is 1000h = 03E8? or is it actually 1000h? So then, is the 200 from sizeof list = 200h or 0c8? Because 1000 + 0C8 = 10C8 (which is the answer)
Title: Re: Need help figuring out calculation of addresses of variables
Post by: jj2007 on March 15, 2016, 05:57:12 PM
Quote from: masterori on March 15, 2016, 05:17:43 PMis the 200 from sizeof list = 200h or 0c8?

200=200
200h=200h
0c8=undefined, no such number format

RTFM.
Title: Re: Need help figuring out calculation of addresses of variables
Post by: dedndave on March 15, 2016, 11:27:41 PM
1000 decimal is 3E8h

windows calculator will make these conversions for you
check the Dec box, put in a value, check the Hex box, the hex value is shown
Title: Re: Need help figuring out calculation of addresses of variables
Post by: FORTRANS on March 15, 2016, 11:55:51 PM
Hi,

   You could let MASM do the counting for you.  Create a listing of
your code.  Sort of the gold standard result.

HTH,

Steve N.