0

In my .bss section I declare array db 5, 4, 3, 2, 1

I then have a pointer ptr defined as %define ptr dword[ebp-8]

I would like to inc and dec this pointer at will to move from one element in the array to another, and then I would like to have to ability to inc the value in the array that the pointer is pointing to, and have this be updated in the array!

I move through the array with a loop in the form of:

mov ptr, array ; not in loop
mov ebx, ptr
mov al, [ebx]
inc ptr

How can I increment the value and then have it saved in the array instead of just some register as if I did inc al , can I do something like inc [ptr] (This doesnt work ofcourse). Is there a better way to approach this entirely?

Thanks

Edit:

I want my array to be something like 10, 8, 6, 5, 2 i.e increment each element by however many

liamod
  • 316
  • 1
  • 9
  • 3
    Just keep your pointer in a register in the first place like a normal person (like EBX). You can of course do `inc byte [ebx]`, or store the updated value of `al` using `mov`. – Peter Cordes Nov 10 '20 at 09:54
  • 1
    And BTW, yes you could do `inc ptr`, which looks funny because you included square brackets in the macro definition. – Peter Cordes Nov 10 '20 at 10:09
  • 1
    Also, if you want to double each element, you should load it, do `add al,al`, then store it. Although IDK why you double the first 3, but add 3 to the `2`, and tack on an extra `1` making a 6-element array when there were originally 5. You can't add new elements to static arrays, unless you already reserved space for them (like some `0` bytes after?) – Peter Cordes Nov 10 '20 at 10:11

1 Answers1

2

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.

Kai Burghardt
  • 1,046
  • 1
  • 8
  • 23