2

New to assembly and very stuck on how do this:

I have a user specified array. The user inputs the length and the characters. My problem is I need move all the vowels to the beginning of the array.

This is my current progress

INCLUDE Irvine32.inc

.data

        ProgInfo BYTE " ",0Ah
                 BYTE "------------------------------------------------------",0Ah
                 BYTE "This program will reorder an array of characters",0Ah
                 BYTE "all vowels will be moved to the beginning of the array",0Ah
                 BYTE "------------------------------------------------------",0Ah
                 BYTE " ",0
        Prompt1 BYTE "Please enter the number of characters to be inputed: ",0
        NChars   DWORD ?    ;Unitialized for user input
        myarray  BYTE ? 

        Prompt2 BYTE "Please enter a string of characters: ",0
        Reordered BYTE " ",0Ah
                 BYTE "The reordered array is: ",0

.code
main PROC
            mov edx, OFFSET ProgInfo      
            call WriteString

            mov edx, OFFSET Prompt1  ;ask for total characters
            call WriteString

            call ReadInt             ;read as integer
            mov Nchar, eax                  

            mov edx, OFFSET Prompt2     ;ask for the string of characters
            call WriteString            

            mov edx, OFFSET myarray
            mov ecx, NChars     
            call ReadString

exit
main ENDP
END main

considering using something similar to this to check for vowels in loop as it goes through the array

VowelCheck:
cmp myarray[edi],'a'
je isVowel
cmp myarray[edi],'A'
je isVowel
cmp myarray[edi],'e'
je isVowel
cmp myarray[edi],'E'
je isVowel
cmp myarray[edi],'i'
je isVowel
cmp myarray[edi],'I'
je isVowel
cmp myarray[edi],'o'
je isVowel
cmp myarray[edi],'O'
je isVowel
cmp myarray[edi],'u'
je isVowel
cmp myarray[edi],'U'
je isVowel
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
J_001
  • 21
  • 5
  • You could have a string with all the vowels and use `repne scasb` to see if the current character can be found in that string. – Michael Mar 14 '16 at 18:12
  • I searched the the class textbook for `repne scasb` and I see how it would be better, however we have not reached that point yet. – J_001 Mar 14 '16 at 19:10
  • 1
    @Michael: A better way to check for vowels is to [use the character as an index into a 32bit-immediate bitmap](http://stackoverflow.com/a/36122634/224132), after range-shifting the ASCII encoding to a zero-based index into the alphabet. For this problem, you don't need to actually sort, just behave sort of like insertion sort. Maintain a vowel pointer to the end of the vowels at the front of the array, and another pointer that scans the whole array. When the second pointer finds a vowel, do a swap and move the vowel pointer. – Peter Cordes Mar 21 '16 at 05:23

1 Answers1

0

I'm not sure if this helps but here is an example of arrays and sorting. This project takes the input from the user and after the last entry it sorts them into numeric order.

INCLUDE Irvine32.inc
INCLUDELIB Irvine32.lib 
.data
       str_0 DB 101 DUP(0)               ; Reserve space for string inputs
       str_1 DB 101 DUP(0)
       str_2 DB 101 DUP(0)
       str_3 DB 101 DUP(0)
       str_4 DB 101 DUP(0)
       str_5 DB 101 DUP(0)
       str_6 DB 101 DUP(0)
       str_7 DB 101 DUP(0)

       str_array DWORD OFFSET str_0,     ; Store addresses of previous strings in array
                                  OFFSET str_1,
                                  OFFSET str_2,
                                  OFFSET str_3,
                                  OFFSET str_4,
                                  OFFSET str_5,
                                  OFFSET str_6,
                                  OFFSET str_7
; Used by sorting algorithm to know if we swapped on an iteration
       swap BYTE 0
.code
main PROC
       MOV ECX, 8
       MOV EAX, 0
READ_STRINGS:
       MOV EDX, [str_array + EAX * 4]    ; Get next string address for input
       MOV EBX, ECX                      ; Preserve ECX
       MOV ECX, 100                      ; Sets max number characters to read in
       MOV ESI, EAX                      ; Preserve EAX
       CALL ReadString
       MOV EAX, ESI                      ; restore EAX
       INC EAX
       MOV ECX, EBX                      ; Restore ECX
       LOOP READ_STRINGS

       ; Strings are all loaded into memory, time to sort
BEGIN_SORT:
       MOV swap, 0                       ; Flag for if we make a swap this pass
       MOV EAX, 0                        ; Current string in str_array

COMPARE_LOOP:
; Loads two string addresses to compare into registers
       MOV EDX, [str_array + EAX * 4]   
       INC EAX
       MOV ECX, [str_array + EAX * 4]

       MOV EBX, 0                        ; Initial location in string
STR_CMP:
; Pull off a byte from the string and load it into EDI
       MOVZX EDI, BYTE PTR [EDX + EBX]         

; If we hit the end of the first string here, assume we
; are less than the other string
CMP EDI, 0
       JE LESS_THAN
       MOVZX ESI, BYTE PTR [ECX + EBX] ; Same thing, but for ESI
       CMP ESI, 0           ; If we hit the end of the second string here, assume
; we are greater than
       JE GREATER_THAN
       CMP EDI, ESI         ; Else we just do a comparison on the ASCII value
; of the indexed character in the string
       JL LESS_THAN
       JG GREATER_THAN
       INC EBX              ; Both characters are equal, move to the next one
; in the strings and do it again
       JMP STR_CMP
LESS_THAN:
       JMP CHECK_END_SORT   ; If the first string is less than the second, they
; are in order, no swapping needed
GREATER_THAN:
       DEC EAX
       MOV [str_array + EAX * 4], ECX
       INC EAX
       MOV [str_array + EAX * 4], EDX ; Swap entries
       MOV swap, 1
CHECK_END_SORT:
       CMP EAX, 7
       JNE COMPARE_LOOP     ; We are not at the end of str_array yet, loop
; again with next two elements

       ; If we are here, we are at end of str_array
       CMP [swap], 0
       JNE BEGIN_SORT       ; If we made a swap in this pass of str_array, do
; another pass from the very beginning

       ; End of sorting

       MOV ECX, 8           ; Number of strings to display
       MOV EAX, 0           ; Current string to dislay
WRITE_STRINGS:
       MOV EDX, [str_array + EAX * 4]
       CALL WriteString
       Call Crlf
       INC EAX
       LOOP WRITE_STRINGS

ENDPRGM:
       CALL Crlf
       CALL WaitMsg

       INVOKE ExitProcess, 0
main ENDP

END main
Patrick
  • 45
  • 6