1

Let me start this by saying that I do know I am not supposed to be brute forcing the program, but I am at a bit of an impasse, the purpose is to use the elements of one array to basically "sort" the elements of another array.

Given:

start = 1

chars: H,A,C,E,B,D,F,G links: 0,4,5,6,2,3,7,0

So you'd start at the first element, A, which is also the number 4 in the links array, which points to the letter B in the chars array, and so on and so forth. The characters are stored in a new array in alphabetical order, where I am having trouble in is getting the index number of the chars array after every step, maybe the code will show a bit more where I am having trouble

   INCLUDE Irvine32.inc

   start = 1

   .data

    chars BYTE 'H','A','C','E','B','D','F','G'
links DWORD 0,4,5,6,2,3,7,0
array BYTE 0,0,0,0,0,0,0,0

.code 
main PROC
mov al, chars +1
mov array, al
mov esi, start          ; moves the start location into the esi register
mov eax, [links + 4]    ; moves the second element of the array into eax
mov dl, chars[eax]      ; moves the character element of the array chars into dl
mov array[esi], dl      ; moves the character into the array
inc esi
mov eax, [links + 16]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 8]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 20]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 12]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 24]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 28]
mov dl, chars[eax]
mov array[esi], dl
inc esi

    main ENDP
    END main

So I think if I just knew how to get the index of the array element after the "links" array points to it I think I could put it into a loop, I just need to know how to do that.

1
  • Just store the offset into a register. Also, use the Indexed Addressing Mode. Commented Oct 22, 2015 at 9:39

1 Answer 1

2
  mov al, chars +1
  mov array, al
  mov esi, start          ; moves the start location into the esi register
  mov ebx, offset links
Again:
  mov eax, [ebx + esi*4]  ; moves the next element of the array into eax
  mov dl, chars[eax]      ; moves the character element of the array chars into dl
  mov array[esi], dl      ; moves the character into the array
  inc esi

Repeat this code the required number of times by testing the ESI register.

You could improve this code by starting at 0 in stead of 1. It would eliminate the 2 lines at the top. It would need modifying the definition of links.

links DWORD 1,4,5,6,2,3,7,0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.