0

This is an exercise from assembly language for intel based computers by Irvine:

What will be the final value of EAX in this example?

   mov eax,0
   mov ecx,10     ; outer loop counter
L1:
   mov eax,3
   mov ecx,5      ; inner loop counter
L2:
   add eax,5
   loop L2        ; repeat inner loop
   loop L1        ; repeat outer loop

My solution is that eax =28 as initially ecx is set to 5, so L2 executes 5 times, so eax= 3+5*5=28. Then ecx becomes 0 and flow moves out of L2, but since ecx=0, L1 would not be repeated and it would move to the next line without repeating L1. So final value of eax=28. I know this is wrong, but can someone tell where is the fault in my thinking?

zx485
  • 28,498
  • 28
  • 50
  • 59
Birbal
  • 61
  • 7

1 Answers1

5

You are correct and there is even a bigger problem in that this code would never end.

    mov     eax, 0              ; Unnecessary as next instruction changes this
L1: 
    mov     eax, 3
    mov     ecx, 5
L2: add     eax, 5
    loop    L2
    loop    L1                  ; ECX is decremented first

So this code would be like the Energizer Bunny and go on forever and the result would be continually 28 over and over again.

    mov     eax, 0              ; Unnecessary as next instruction changes this
L1: push    ecx                 ; This and the pop below solves the problem
    mov     eax, 3
    mov     ecx, 5
L2: add     eax, 5
    loop    L2
    pop     ecx
    loop    L1                  ; ECX is decremented first

Now the program will at least end, but the result is still only going to be 28. If it needs to be 253 like I think the objective is, then you should be able to see the one instruction you need to move to make that happen.

zx485
  • 28,498
  • 28
  • 50
  • 59
Shift_Left
  • 1,208
  • 8
  • 17
  • and what is that instruction Shift_Left? – Birbal Feb 11 '18 at 09:39
  • also, i didn't get why it would go on forever and the result would be 28 over and over again? – Birbal Feb 11 '18 at 09:44
  • I get why it would go on forever as ecx=0 when it enters loop L1 statement and that makes it go again and again infinitely. – Birbal Feb 11 '18 at 09:53
  • @Birbal because the `mov eax,3` in the most inner loop will reset `eax` every time to `3`... so at the end of inner loop (after `loop L2` will count down from 5 to 0 in `ecx`) the `eax` is equal to `28`. Then you do `loop L1` which will do `--ecx` -> `0` -> `-1 == 0xFFFFFFFF`, that's not zero, so it will jump to `L1` infinitely, and there you will reset `eax` from `28` back to `3`. -> rather load this into debugger, and single step over it (make sure you use some kind of "step into/trace" command over `loop` to not "execute" it fully until `ecx == 0`, that will end in infinite loop too). – Ped7g Feb 11 '18 at 09:54
  • @Ped7g okay I get it now. – Birbal Feb 11 '18 at 09:57
  • @Ped7g: How to edit the above code in a way that outer loop counter is not erased when inner loop is started. – Birbal Feb 11 '18 at 10:06
  • 1
    @Birbal: This answer already shows how to use `push` / `pop` to save/restore ECX so you can use [the slow `loop` instruction](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) for both loops. i.e. to multiplex the two loop counters into the same register. Obviously the better way is to use two different registers as loop counters, like `dec edx / jnz inner_loop` / `dec ecx / jnz outer_loop` – Peter Cordes Feb 11 '18 at 10:11
  • 2
    @Birbal delete all lines, problem solved. If the code is required to return value of 10*5*3, then delete all code, and use `mov eax, 150`, problem solved. If you didn't catch my drift, without explaining+fixing the requirements, what should be result, there's no way to fix it, because it is not clear what that `mov eax,3` should do there in the inner loop. Overall forcing students to learn about nested loop in this way is a bit lacking imagination and purpose. Could have rather show you how to fill MxN array with each line going 0123... but +1 for first value (second line 123.., third 234...). – Ped7g Feb 11 '18 at 10:33