[ELI5] I need help in assembly x86

222 views

I wrote a basic code, here it is in summery:
**data segment:**
arr db 1
atr db 100

**code segment:**
mov bl,arr
mov bl,\[arr\]

for some reason the two lines in the code segment do the same thing, is that what they’re supposed to do? I am sorry if its a dumb question I’ve just began learning, but from what I understood up til now is that for example \[arr\] where arr is for example “1” would return the value in the address of “1” whatever that may be(although I thought it would return the value of atr since its located in ds:\[1\]) and not the value “1”. What am I doing wrong?

In: 0

2 Answers

Anonymous 0 Comments

First, `arr` is not 1, it is a memory address, where this 1 is located. So when you write `mov bl, [arr]`, you command the CPU to read from the memory at the `arr` label (which initially contains 1, but can change during a program run).

If you want to store an address in memory – you have to use two instructions: first read the address, second – read the value at that address. Also note, that address is 2 bytes in 16 bit mode, 4 bytes in 32 bit, and 8 bytes in 64 bit mode, so you should declare it with `dw`, `dd`, or `dq` respectively. You also use a longer register – `bx`, `ebx`, or `rbx`:

.data
arr dw atr ;use ‘dd’ or ‘dq’ in 32/64 bit.
atr db 100
.code
mov bx,[arr] ;use ‘ebx’ or ‘rbx’ in 32/64 bit
mov al,[bx]

Second, there is no single x86 assembly. Each assembler program has its own, slightly different syntax.

* In Netwide Assembler (NASM), `mov bl,arr` moves an address of `arr`, while `mov bl,[arr]` reads from that address.
* In Borland Turbo Assembler (TASM) and Microsoft Macro-Assembler (MASM), both mean the same – read from the address `arr`. To move an actual address, you have to write `mov bx,offset arr`.

You are viewing 1 out of 2 answers, click here to view all answers.