In my .bss
section I declare array db 5, 4, 3, 2, 1
This does not make sense.
By default .bss
is defined as nobits
, i. e. an uninitialized section (although a modern multi-user OS will initialize it with some values [usually zero] to prevent data leaks).
You probably meant to write .data
section.
nasm(1)
will issue an “ignored” warning about any db
in a nobits
section.
I then have a pointer ptr
defined as %define ptr dword[ebp‑8]
I would like to inc and dec this pointer […]
No, you have a (case-sensitive) single-line macro ptr
.
Any (following) occurrence of ptr
will be expanded to dword[ebp‑8]
.
Use nasm ‑E source.asm
(preprocess only) to show this.
[…] then I would like to have to ability to inc
the value in the array that the pointer is pointing to […]
Your ptr
macro says it’s pointing to a dword
– a 32‑bit quantity – but your array
consists of db
– data Byte, 8‑bit quantity – elements.
This doesn’t add up.
I want my array to be something like 10, 8, 6, 5, 2
i.e increment each element by however many
Well, x + x = 2 × x, calculating the sum of a value plus the same value is the same as multiplying by two.
Any decent compiler will optimize multiplying by a constant factor 2ⁿ as a shl x, n
.
Unless you need certain flags (the resulting flags of shl
and add
marginally differ), you can do something like
lea ebx, [array] ; load address of start of `array`
xor eax, eax ; eax ≔ 0, index in array starting at 0
mov ecx, 5 ; number of elements
head_of_loop:
shl byte [ebx + 1 * eax], 1 ; base address + size of element * index
add eax, 1 ; eax ≔ eax + 1
loop head_of_loop ; ecx ≔ ecx − 1
; if ecx ≠ 0 then goto head_of_loop
The constant factor in [ebx + 1 * eax]
can be 1
, 2
, 4
or 8
.
Note, the loop
instruction is utterly slow and was used just for the sake of simplicity.