How to create nested loops in x86 assembly language
Sure, it's possible. Since every computer program eventually boils down to assembly - it is naturally the most powerful language possible (excluding direct bit manipulation).
The actual code depends on your system, compiler and applied optimizations, but basically it should be something like this (example for 2 nested loops, not 3):
mov ecx, 0
outerLoop:
cmp ecx, 10
je done
mov ebx, 0
innerLoop:
mov eax, ecx ; do your thing here
add eax, ebx
cmp ebx, 10
je innerLoopDone
inc ebx
jmp innerLoop
innerLoopDone:
inc ecx
jmp outerLoop
done:
Note, you don't need local variables, you've got general-purpose registers for the usage that you need. If you insist on having variables, you can use memory addresses for that and read/write using register pointers.
Anderson Green
I write source-to-source compilers in JavaScript using Peggyjs. I also write compilers in Prolog. For reference, I also have a list of source-to-source compilers on GitHub.
Updated on July 09, 2022Comments
-
Anderson Green almost 2 years
Is it possible to create nested loops in x86 assembly language?
I'd like to translate this psedocode into correct x86 assembly code (using MASM syntax), but I'm not sure how to initialize each loop counter here. Is it even possible to declare local variables in x86 assembly (as in most other programming languages)?
for (var i = 0; i < 10; i++){ for(var j = 0; j < 10; j++){ for(var k = 0; k < 10; k++){ mov eax, i + j + k; } } }