and sorry for the title, i couldn't imagine a way to express this in english. So i'm writing a little game in assembly for a course, and, inside of two for loops, i copy the pixel data from the "bomb" vector of pixels, to the "area" vector of pixels, which will later be drawn to the screen. In C, it should look like this:
int x, y;
for(int i=0;i<32;i++)
for(int j=0;j<32;j++)
area[x+i][y+j]=bomb[i][j];
Using masm assembler and notepad++ to write the code, i got
mov ecx, 0 ; my i
outerloop:
cmp ecx, 32
je done
mov esi, 0 ; my j
innerloop:
mov ebx, sprite_width ; sprite_width=32
mov eax, ecx
mul ebx
add eax, esi
shl eax, 2
mov edi, bomb
add edi, eax
mov pixel, edi ; pixel = bomb[i][j]
mov ebx, area_width
mov eax, y
add eax, ecx
mul ebx
add eax, x
add eax, esi
shl eax, 2
mov edi, area
add edi, eax
mov eax, [pixel]
mov dword ptr [edi], eax ; should be area[x+i][y+j] = pixel
cmp esi, 32
je innerloopdone
inc esi
jmp innerloop
innerloopdone:
inc ecx
jmp outerloop
done:
After this, the 32x32 area in the screen should look like a bomb, because of how the bomb vector is written, however i only see some green and some blue. Blue is not even in the bomb vector at all. Is there any mistake in the loop/logic?
As you didn't post definitions of those symbols, I'm just guessing how they were defined, I would expect something like:
Then this should be enough to draw non-transparent piece of graphics in a bit more efficient way than your proposal:
I didn't debug it, so there may be a problem, but I don't have MASM to try.
About your original code, there's recurring theme where you write things like:
vs
Unfortunately in MASM these are identical, the
[]
memory access is not needed when you usesymbol
name, i.e. themov edi,bomb
is loading value from memory, not getting address of bomb. To get address usemov edi,OFFSET bomb
in MASM, orlea edi,[bomb]
.So you mix up those, and sometimes it looks like you did want to get memory address, and sometimes value, and sometimes you store address into memory instead of value, etc...
Also I strongly suggest you to start using
[]
around symbols even when the MASM doesn't require them, so the lines in code accessing memory are visually easier to spot, and the style is identical with indirect addressing through registers (i.e. same style asmov eax,[ebx]
, where even MASM needs square brackets). Check my example, how I consistently use[]
around every memory access (exceptlea
, where the CPU will not access memory, only calculate address, but that's due to nature oflea
instruction itself, the argument for it is still memory-access-type).But most importantly find some working debugger for your platform, and learn to use it, because programming in assembly without debugger is like trying to assemble robot blindfolded. You can do it, but it's much more difficult and requires lot of skill and experience. This is your major problem right now, the code style and efficiency of your code is like 10% of your problem, not being able to debug your code is 90%.