2

When you are looping backwards in Assembly x86, what is currently happening in the memory (Can you try to be visual, thanks)? The following code is what I am currently wondering about:

INCLUDE Irvine32.inc
.data
   arrayb byte 1,2,3,4,5,6 ;6-7 bytes 
   len dword lengthof arrayb
   space byte " ",0
   x dword 3



.code
main PROC
    mov edx,offset space
    mov eax,0  ; clear ecx of garbage
    mov ecx, len
    mov esi,offset arrayb ; start of the array's memory
    add esi,len ;This causes the array value to start at 6 
    dec esi      ; esi goes from esi+5,esi+4,...,esi
    myloop2:
       mov al,[esi]
       call writedec
       call writestring
       dec esi
     loop myloop2
     call crlf

In particular, why did I have to add 1 to esi? When you add 1 to the high speed memory transfer register esi, it seems that it causes the array value to start at 6. Why is that?Thank you.

Johan
  • 74,508
  • 24
  • 191
  • 319
jack black
  • 195
  • 1
  • 2
  • 11
  • 3
    that `l` is lowercase L, not the number 1. lowercase L (`l`) is the length of the array declared as `l dword lengthof arrayb`. `mov ecx, l` moves the length (number of elements) of the array `arrayb` into _ECX_ (value 6) and `add esi,l` adds 6 to _ESI_ which takes it one past the array itself. `dec esi` backs it up 1 to the last element in the array. – Michael Petch Sep 25 '16 at 04:09
  • 1
    This is why descriptive variable names and proper fonts are important... – Sami Kuhmonen Sep 25 '16 at 04:18
  • Thank you so much for clearing that up. Can you explain how the memory works for lengthof? I am very confused with the size of memory. Thank you. – jack black Sep 25 '16 at 04:21
  • MASMs lengthof tries to be intelligent. It looks at `arrayb` and counts how many elements you defined (6 bytes and the elements being the number 1 2 3 4 5 6). You can learn more here: http://www.oopweb.com/Assembly/Documents/ArtOfAssembly/Volume/Chapter_8/CH08-5.html – Michael Petch Sep 25 '16 at 04:23
  • Why would you move the length of the array into ECX when that is a counter loop register? Also, I just want to clarify if I am stating this correctly: I add 32 bits (dword) into ESI register, which will take up all the space. However the dec esi will decrement the memory down to 31 bits?Is that correct? I'm sorry but I am new to assembly. Thank you – jack black Sep 25 '16 at 04:25
  • `Dec` subtracts 1 from the value in _ESI_. _ESI_ happens to be used as a pointer to bytes. – Michael Petch Sep 25 '16 at 04:37
  • The 32-bit registers are all general purpose. You can use any of them for anything, unless you're using one of x86's legacy instructions that requires an input in a certain register. (like [DIV](http://www.felixcloutier.com/x86/DIV.html), or [REP MOVS](http://www.felixcloutier.com/x86/REP:REPE:REPZ:REPNE:REPNZ.html), which you aren't using.) – Peter Cordes Sep 25 '16 at 04:37
  • @MichaelPetch: Is there a real question here other than the typo? I haven't read all the comments, but the actual text of the question doesn't make much sense. – Peter Cordes Sep 25 '16 at 04:40
  • @PeterCordes Not a typo, they are misreading lowercase `L` for a `1` (number one). But I'd classify this in the category of typos. I think misreading it probably caused most of the issues in understanding. The followup comment by the OP suggests there are other issues in understanding. – Michael Petch Sep 25 '16 at 04:41
  • 1
    _"Why would you move the length of the array into ECX when that is a counter loop register?"_ That question essentially translates into _"Why would you move the length of the array into ECX when you want to initialize a loop counter with the length of the array?"_ Because that's what you wanted. – Michael Sep 25 '16 at 12:06

0 Answers0