News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Trouble understanding where values are coming from

Started by RedSkeleton007, September 07, 2015, 02:47:27 PM

Previous topic - Next topic

RedSkeleton007

Given the following variable definitions:

.data
var2 WORD 1000h, 2000h, 3000h, 4000h
var3 SWORD -16, -42


The value, according to the book, the following instructions calculate to:

mov ax,[var2 + 4] ;3000h Why is this not 1000h, (since the last element in the var2 word list is 4000h at index [3]) wouldn't the [4] have no where to go but 1000h at index 0?

mov ax,[var3 - 2] ;4000h ;Where is 4000h coming from? It's not evened signed, unlike -16 and -42

zedd151

Quote from: RedSkeleton007 on September 07, 2015, 02:47:27 PM
Given the following variable definitions:

.data
var2 WORD 1000h, 2000h, 3000h, 4000h
var3 SWORD -16, -42


The value, according to the book, the following instructions calculate to:

mov ax,[var2 + 4] ;3000h Why is this not 1000h, (since the last element in the var2 word list is 4000h at index [3]) wouldn't the [4] have no where to go but 1000h at index 0?

mov ax,[var3 - 2] ;4000h ;Where is 4000h coming from? It's not evened signed, unlike -16 and -42

Okay imagine those variables as a list:

1000h ; offset 0
2000h ; offset 2
3000h ; offset 4
4000h ; 0ffset 6
-16      ; offset 8
-42      ; offset 10

It has to do with 'indexing'

I don't have a good technical explanation, but that should give you a good idea

zedd151

You might want to look up the "lea" instruction as well

The way you have the code, it is referencing the address where var2 is at for the first one,

and the ADDRESS of var3 for the second.

word values are 2 bytes long.

so mov ax, [var2 + 0] == var2 == 1000h
mov ax, [var2 + 2] == 2000h
mov ax, [var2 + 4] == 3000h
mov ax, [var2 +6] == 4000h

and continuing on the same path,

mov ax, [var2 +   8] == -16
mov ax, [var2 + 10] == -42
mov ax, [var2 + 12] == whatever word size value follows
----------------------------------

which means that

mov ax, [var3 - 2] == 4000h is correct

and also means that

mov ax, [var3 + 2] would equal -42


zedd151

mov ax, word ptr [var2 + 4]

is NOT the same as

mov ax, var2
add ax, 4

----------------------------

It is the same as
-------------------------------
mov eax, offset var2
add eax, 4
mov ax, word ptr [eax]
--------- or ---------------
lea eax, var2
add eax, 4
mov ax, word ptr [eax]
--------------------------------




I think what you want or expect is:

---------------------------
    add word ptr [var2], 4 ; changes the value of var2
    mov ax, var2
  - - - - - - - - - - - - -
or even more simply
- - - - - - - - - - - - - -
    add var2, 4                ; changes the value of var2
    mov ax, var2
- - - - - - -- - - - - - -

   or by adding in ax

- - - - - - - - - - - - - -
   mov ax, var2              ; does not change the value of var2
   add ax, 4
---------------------------

if I am not mistaken

Do some more research, you'll get the hang of it

Hope this helps

edit = clarification, and more clarification.

zedd151


jj2007

Quote from: zedd151 on September 07, 2015, 03:27:35 PM

Okay imagine those variables as a list:

1000h ; offset 0
2000h ; offset 2
3000h ; offset 4
4000h ; 0ffset 6
-16      ; offset 8
-42      ; offset 10

It has to do with 'indexing'

Red,

The secret is that asm != C 8)

There is no automatic indexing in assembler, so var[2] is "what you find at byte offset 2", not "element 2, i.e. the third element".

Re signed: The value -42 can be interpreted as signed or unsigned. A mov ax, -42 does not impress the register ax, it just sees 0000FFF0h. Your print routine may then decide whether fff0h is "-42" or "+65520". You can force an interpretation with the movzx and movsx instructions.

include \masm32\MasmBasic\MasmBasic.inc
.data
var2 WORD 1000h, 2000h, 3000h, 4000h
var3 SWORD -16, -42

  Init
  mov ax, var2[2]
  mov dx,[var3 - 2]
  movsx ecx, [var3 + 2]
  movzx esi, var3
  movsx edi, var3
  deb 1, "Test", x:ax, x:dx, ecx, x:esi, x:edi, esi, edi ; x: means "use hex"
  Exit
end start


Output:
x:ax            2000
x:dx            4000
ecx             -42
x:esi           0000FFF0
x:edi           FFFFFFF0
esi             65520
edi             -16

K_F

Quote from: RedSkeleton007 on September 07, 2015, 02:47:27 PM
Given the following variable definitions:

.data
var2 WORD 1000h, 2000h, 3000h, 4000h
var3 SWORD -16, -42


The value, according to the book, the following instructions calculate to:

mov ax,[var2 + 4] ;3000h Why is this not 1000h, (since the last element in the var2 word list is 4000h at index [3]) wouldn't the [4] have no where to go but 1000h at index 0?

mov ax,[var3 - 2] ;4000h ;Where is 4000h coming from? It's not evened signed, unlike -16 and -42
If you look at the declaration below.. it's an array of WORD (16 bits) sized values
var2 WORD 1000h, 2000h, 3000h, 4000h

The instruction says ...
mov ax,[var2 + 4]    -->     [var2 + 4] means Address of Var2 plus 4 bytes offset (offsets are always in Bytes)

Move a 16 bit value (you using the AX register which is 16 bits wide (EAX is 32 bits)) from the [address] (Address of Var2 with 4 bytes added to it) to AX.
As a byte is 8 bits wide and each array value  is (WORD) 16 bits wide.. so 8x4 = 32 bits, but 16x2 = 32 bits.. so the [address] points to 2 variables further on in the array (=3000h)

Var2 + 0 = 1000h
Var2 + 2 = 2000h
Var2 + 4 = 3000h

Now the same idea with
mov ax,[var3 - 2]

..which is nothing more than 2 bytes backwards = 1x 16 bit array value before Var3... === 4000h, which is the last value of the Var2 array.
;)
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

K_F

Damm.. wrong button.. how  does one delete.. :biggrin:

Edt: = Wrong button pressed :greensml:

Quotedelete contents of the post
She.. no worka !
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

zedd151

Quote from: K_F on September 07, 2015, 05:36:00 PM
Damm.. wrong button.. how  does one delete.. :biggrin:

Click on Modify
delete contents of the post :biggrin:

I also always try to add a footnote
edit = <reason>

:biggrin:


jj2007

Try Art of Asm for comparison, especially chapter 4 onwards. And attention, both books have a 16-bit history and are likely to focus on obsolete 16-bit stuff. See also http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm, Iczelion tutorials.

RedSkeleton007

Quote from: jj2007 on September 09, 2015, 11:25:34 AM
Try Art of Asm for comparison, especially chapter 4 onwards. And attention, both books have a 16-bit history and are likely to focus on obsolete 16-bit stuff.
No offense dude, but my book and the art of assembly have a 14 year difference, so I don't even understand why you would bother comparing them. Also, I want to learn MS-DOS and 16-bit stuff eventually, because video games for the Super Nintendo run on 16-bit assembly, and are thus very fun to hack. In fact, I've been a member of SMW Central far longer than I've been a member of this site ;)

jj2007

Quote from: RedSkeleton007 on September 09, 2015, 12:09:56 PMvideo games for the Super Nintendo run on 16-bit assembly, and are thus very fun to hack

That makes sense :t

dedndave

i don't think super nintendo uses intel 8086 processors
so, you are in for a lesson about segmented memory that you may not use elsewhere

it uses a processor that's an off-shoot from rockwell 6500 series

RedSkeleton007

Quote from: dedndave on September 09, 2015, 09:12:22 PM
i don't think super nintendo uses intel 8086 processors
so, you are in for a lesson about segmented memory that you may not use elsewhere

it uses a processor that's an off-shoot from rockwell 6500 series
That's true. The CPU of the SNES has a 65c816 core, so SNES programming is done with 65c816 assembly. But asm is still asm. My hope is that someday I will be able to port 16-bit MS-DOS games onto an SNES cartridge. Imagine Jazz Jackrabbit on the SNES 8)