2

I am trying to fill an array with 10 numbers, 1 through 10. In the loopArray below, i load a 32 bit DWORD size array with the 32 bit register ebx, which is incremented by 1 each iteration. In PrintLoop2 i am loading ecx with each element of the array so that i can see these values in visual studio, and confirm that my array is filled with the values 1-10. My problem is that ecx is showing huge values like "04030201" on the first loop and "05040302" on the second, ect. What am i doing wrong?

.586
.MODEL FLAT

.STACK 4096                 
n=10
.data
prime DWORD n DUP(?)
.code
main PROC

mov ebx, 1
loopArray:
    mov prime[ebx], ebx
    cmp ebx, n
    inc ebx
jb  loopArray

mov ebx, 1
mov ecx, 0
PrintLoop2:
    mov ecx, prime[ebx]
    cmp ebx, n
    inc ebx
jb  PrintLoop2
Some_Dude
  • 309
  • 5
  • 21
  • 1
    Each dword is 4 bytes. You need to scale `ebx` by 4, e.g. `[prime+ebx*4]`. – Jester Feb 28 '18 at 16:10
  • I thought ebx was 32 bits, which is 4 bytes. It even says in my textbook "Each of EAX, EBX, ECX, and EDX is 32 bits long." so i thought that i would fit without scaling. Could i just shrink the size of the prime array to fit without scaling? – Some_Dude Feb 28 '18 at 16:14
  • 2
    `ebx` **is** 32 bits, but you don't store `ebx` into memory, you use it as index (address) (in the print loop I mean). And your code operates with elements of the array like they are 32 bit (`prime DWORD n DUP`), so each element occupies 32 bits in memory, and memory is addressable by bytes (i.e. 32 bits = +4 address offset). To lessen memory usage, use smaller element data type, if prime element can be just true/false, you can use single bit (with extra code to address separate bits instead of bytes). If you want to store integers, then for 0..255 = 1B, 0..65535 = 2B, 0..4294967295 = 4B, etc. – Ped7g Feb 28 '18 at 16:21
  • 1
    For a 1..10 array you can thus use single bytes, i.e. `prime BYTE n DUP (?)` and setup `mov [prime+ebx],bl` ... and print: `movzx ecx, BYTE PTR [prime+ebx]` .... just one more note, the `mov prime[ebx],ebx` is not like C or C++, which is doing pointer math for you, the `[ebx]` part is byte offset, not array index, so that needs to be fixed by `*4` scaling to work with `DWORD` array elements. – Ped7g Feb 28 '18 at 16:24
  • This answers my question, Thanks a lot!! – Some_Dude Feb 28 '18 at 17:23

0 Answers0