-1

I am new to ASM. I have question regarding how data saved in memory.

Here is my ASM 16 bit code

;db.com
.model small
.code
org 100h
start:
jmp proses
A db '123'
B dw 0abcdh
proses:

int 20h
end start

Then I try to debug with -d command

enter image description here

The above picture shown that A variable in correct order in hexa value, but B variable in CD then AB.

My question is why data in A variable saved differently with B variable can you please explain me?

fat
  • 6,435
  • 5
  • 44
  • 70
Dark Cyber
  • 2,181
  • 7
  • 44
  • 68
  • 1
    You are observing what happens because x86 is a little endian system. `A db '123'` simply stores 3 bytes in a row. When you move beyond data elements that are more than one byte in size the bytes are stored in reverse order. `B dw 0abcdh` is a 16-bit value so the bytes of the word are stored in memory in reverse order, so in memory it appears as `cd ab`. If you had `C dd 11223344h` the bytes would be stored in reverse order as `44 33 22 11` – Michael Petch Mar 14 '16 at 08:48
  • See [endianness](https://en.wikipedia.org/wiki/Endianness) in wikipedia. – Ruslan Mar 14 '16 at 09:03
  • 1
    The case of an instruction sequence is a little more complicated. It begins with the instruction opcode, and if there is a 16-bit operand, that will be in little-endian order, ie the least significant byte first. Instruction sequences always begin with the opcode (regardless of endianness) otherwise the processor won't know how many bytes comprise the instruction. – Weather Vane Mar 14 '16 at 09:31

1 Answers1

1

x86 is using little endian, so word will be stored as low-byte, high-byte and dword as low-word, high-word

0x1020 will be 0x20 0x10 in memory
and 0xabcd1234 will be 0x34 0x12 0xcd 0xab

by defining db <string> you order the assembler to use the string as a sequence of bytes, and each byte is stored in the same order, one by one

so e.g.
db '012345",13,0 will be 0x30 0x31 0x32 0x33 0x34 0x35 0x0D 0x00

Fifoernik
  • 9,779
  • 1
  • 21
  • 27
Tommylee2k
  • 2,683
  • 1
  • 9
  • 22