0

I'm trying to put together a BYTE array of chars and a DWORD array of numbers and reverse them into a QWORD array. so if 1,2,3,4,5 and s,h,r,u,g are entered, it will print out g5 u4 r3 h2 s1. I keep getting weird garbage values for only ONE of the arrays. Only one will print like it should. If I change one of the arrays to a copy of the other, it works perfectly fine.

include irvine32.inc

.data
string1 byte 0Ah,0Dh,"Dumping out charArr",0
string2 byte 0Ah,0Dh,"Dumping out numArr", 0
string3 byte 0Ah,0Dh,"Dumping out newArr", 0

charArr byte   5 dup  (?)
numArr  dword  5 dup  (?)
newArr  qword  5 dup  (?)

prompt1 byte "Please enter a number: ", 0
prompt2 byte "Please enter 5 characters: ", 0
prompt3 byte "The integer array you entered is: ", 0
prompt4 byte "The characters you entered are: ", 0
prompt5 byte "The mean of the integer array is: ", 0
prompt6 byte "The elements of the new array are: ", 0
prompt7 byte " ", 0

mov esi, offset numArr              ; number input
mov ecx, lengthof numArr
mov edx, offset prompt1
L1:
call writeString
call readDec
mov [esi], eax
add esi, type numArr
loop L1

mov esi, offset numArr              ; array output
mov ecx, lengthof numArr
mov edx, offset prompt3
call writeString
L3:
mov eax, [esi]
call writeDec
add esi, type numArr
mov al, ' '
call writeChar
loop L3
call crlf

mov esi, offset charArr             ; char input
mov ecx, lengthof charArr
mov edx, offset prompt2
call writeString
L2:
call readChar
mov [esi], al
add esi, type charArr
loop L2
call crlf

mov esi, offset charArr             ;char output
mov ecx, lengthof charArr
mov edx, offset prompt4
call writeString
L4:
mov eax, [esi]
call writeChar
add esi, type charArr
mov al, ' '
call writeChar
loop L4
call crlf

sub eax, eax                        
sub edx, edx
mov esi, offset numArr
mov ecx, lengthof numArr
L5:
add eax, [esi]
add esi, type numArr
loop L5
mov ecx, lengthof numArr
div ecx
mov ebx, edx
mov edx, offset prompt5
call writeString
call writeDec
call crlf


; reversing and combining arrays

mov esi, offset newArr                      
mov edi, offset charArr + type numArr       
mov ecx, lengthof newArr                    
mov edx, offset prompt6                     
call writeString                            
L6:                                         
    mov eax, [edi]                      
    dec edi                             
    mov [esi], eax                      
    add esi, type newArr            
    loop L6


mov esi, offset newArr + type numArr    
mov edi, offset numArr + type numArr * type numArr          
mov ecx, lengthof newArr                                
L7: 
mov eax, [edi]                  
sub edi, type numArr            
mov [esi], eax                  
add esi, type newArr            
loop L7


    ;printing
mov esi, offset newArr          
mov ecx, lengthof newArr        
mov edx, offset prompt7
L8: 
mov eax, [esi]                  
call writeChar                  
add esi, type numArr            
call writeDec
add esi, type numArr
call writeString
loop L8
  • `offset charArr + type numArr` looks very suspicious. Why does that make any sense for looping backwards over the characters, except for the coincidence that element 4 is the last character and your `numArr` has 4-byte elements? – Peter Cordes Oct 31 '17 at 08:06
  • Also, [the `loop` instruction basically sucks (it's slow)](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) and isn't worth using. Especially here; you're basically wasting `ecx` here when you could compare a pointer against an end-pointer. Also, give your labels meaningful names. Don't try to imitate a compiler and just use numbered labels. – Peter Cordes Oct 31 '17 at 08:09
  • Why are you summing an array in `L5`, and dividing by the length? The question doesn't say anything about an average. I don't even see where you're trying to copy from `numArr` into `newArr`. Comment your code, and make a [mcve]. Also single-step through it with a debugger so you can see what's happening. – Peter Cordes Oct 31 '17 at 08:14
  • Oh, averaging was also a part of the assignment, sorry haha. Anyways, I was able to fix the issue by changing part of the last loop. `numArr` goes into `newArr` with L7. Thanks so much for the feedback, I'll be sure to make it more understandable next time. – thecrentist Oct 31 '17 at 16:45

0 Answers0