how to display the count in MASM? - masm

this is my code for a MASM assignment, the assignment is:
Write an application that does the following:
Declare a 32-bit integer array with 50 elements with uninitialized values
Fill the 32-bit array with 50 random integers
Loop through array, and display each value, and count the number of negative values
After final loop finishes, display the count
I have it doing everything but displaying the count of negative numbers. If anyone can tell me where I am wrong that would be super helpful I have been working on this for some time.
INCLUDE Irvine32.inc
.data
myArray SWORD 50 DUP(?)
count DWORD 0
.code
main proc
call randomize
mov esi, offset myArray
mov ecx, lengthof myArray
L1:
call Random32
call WriteInt
call Crlf
mov [esi], eax
add esi, 4
loop L1
mov eax, offset myArray
mov esi, lengthof myArray
L2:
cmp dword ptr[esi], 0
jge L3
DWORD count
L3:
add esi, 4
loop L2
mov eax, count
call WriteDec
call Crlf
exit
main ENDP
END main

Related

How can I print the contents of an array?

I'm trying to print the contents of an array by using assembly language as below.
I could compile the code, but I could not run it.
How should I fix the code to print the contents of an array?
TITLE arrayFill_example (arrayFill_ex.asm)
INCLUDE Irvine32.inc
.data
count = 5
array DWORD count DUP(?)
arraySize = ($ - array) / 4
.code
; saves the general-purpose registers, retrieves the parameters, and fills the array
ArrayFill PROC
push ebp
mov ebp,esp
pushad ; save registers
mov esi,[ebp+12] ; offset of array
mov ecx,[ebp+8] ; array length
cmp ecx,0 ; ECX == 0?
je L2 ; yes: skip over loop
L1:
mov eax,10000h ; get random 0-FFFFh
call RandomRange ; from the link library
mov [esi],ax ; insert value in array
add esi,TYPE WORD ; move to next element
loop L1
L2: popad ; restore registers
pop ebp
ret 8 ; clean up the stack
ArrayFill ENDP
main PROC
push OFFSET array ; passed by reference
push count ; passed by value
call ArrayFill
; for showing array contents
mov eax, 0
mov esi, array
mov ecx, arraySize
L1:
mov eax, array[esi * TYPE array]
call WriteInt
call Crlf
add esi, 4
loop L1
exit
main ENDP
END main
Specifically this part is not working for me...
; for showing array contents
mov eax, 0
mov esi, array
mov ecx, arraySize
L1:
mov eax, array[esi * TYPE array]
call WriteInt
call Crlf
add esi, 4
loop L1
Problem 1
array DWORD count DUP(?)
With this definition the array contains dwords. But your program just fills the array with words using:
mov [esi],ax ; insert value in array
add esi,TYPE WORD ; move to next element
Better write:
mov [esi], eax ; insert value in array
add esi, 4 ; move to next element
Problem 2
mov esi, array
...
mov eax, array[esi * TYPE array]
These lines are redundantly referring to the array. That's adding a pointer to a pointer, giving the wrong address! (Or actually, mov esi, array loaded the first element, not the address, because that's how MASM syntax works.)
mov esi, OFFSET array gives you the address in esi. From there, [esi] is the first element, array[esi] is similar to C array[ (intptr_t)array ] (but just a byte offset without scaling by the element size). The resulting address is unlikely to be valid.
Just use one or the other, indexing with small integers, or a pointer increment. Getting a pointer into a register is usually good, as in:
mov esi, OFFSET array
mov ecx, arraySize
L1:
mov eax, [esi]
call WriteInt
call Crlf
add esi, 4
loop L1

Looping backwards in Assembly x86

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.

Assembly Language x86 Irvine32

I'm fairly new to Assembly Language and I'm trying to figure out this program. Just want to know if I'm on point with the program. How do I correct this program?
Write a loop that computes the sum of all the elements in the array of bytes. Print the result. Some hints: Load the size of the array into an appropriate register. Load the offset of the current element of the array and change it accordingly on every iteration of the loop.
Here's what I have so far:
INCLUDE Irvine32.inc
.data
val1 BYTE 1,2,3
counter = 0
.code
main PROC
mov ax, 0
mov ax, (LENGTHOF val1)
mov ax, OFFSET Counter
movzx ecx,ax
L1:
add eax, val1[ecx]
inc eax
loop L1
Call WriteDec
exit
END PROC
end main
You have several errors in your code: In the following sequence you repeatedly set ax which is pretty useless:
mov ax, 0 ; you set ax to 0
mov ax, (LENGTHOF val1) ; you set ax to 3
mov ax, OFFSET Counter ; you set ax to an address (and try to use a 16-bit register for a 32-bit address
Then you add this offset to another offset in
movzx ecx,ax
L1:
add eax, val1[ecx] ; add offset(val1)[offset(Counter)] to offset(Counter)
With certainty, this will give you a memory error, because the address may be anywhere. Then you increase this offset with
inc eax ; you probably confused this with a counter/index register
And after that you use this offset in ECX, which you put in there by movzx ecx, ax as an index in ECX in the LOOP instruction
loop L1 ; decrements ECX and loops if ECX != 0
After fixing all of these errors, the code could look like this:
INCLUDE Irvine32.inc
.data
val1 BYTE 1,2,3
counter = 0
.code
main:
xor eax, eax ; 32-bit register for the sum
xor edx, edx ; 32-bit register for the counter/index
mov ecx, LENGTHOF val1 ; number of entries in fixed size array in ECX as maximum limit
L1:
movsx ebx, byte ptr val1[edx] ; extend BYTE at offset val1 + EDX to 32-bit
add eax, ebx ; add extended BYTE value to accumulator
inc edx ; increase index in EDX
loop L1 ; decreases ECX and jumps if not zero
Call WriteDec ; I assume this prints EAX
exit
end main

How to move and display array values from one to another in Assembly Language?

I am trying to move the values in Array1 to Array2, and then display them. I have been working on this and could not figure it out at all. Would anyone please help me? Thanks
INCLUDE Irvine32.inc
INCLUDE macros.inc
.data
Array1 DWord 2,4,6,8,10
Array2 DWord 5 Dup(0)
.code
main PROC
mov edx, OFFSET Array1
mov esi, OFFSET Array2
mov ecx, LENGTHOF Array1
mov eax, 0
Call Dumpregs
Call Dumpregs
L1:
mWrite "Hello"
Call CRLF
Loop L1
Call Dumpregs
L2:
mov eax, [edx]
mov [esi], eax
add esi, 4
add edx, 4
Loop L2
exit
main ENDP
END main
Your L2 loop cannot produce the desired result since the preceding code wiped ECX clean (You used loop L1). To copy the array you need to re-initialize ECX. Also it's best to setup the pointers EDX and ESI close to this L2 loop because perhaps there is a risk of them being modified by all those preceding (macro)calls!
mov edx, OFFSET Array1
mov esi, OFFSET Array2
mov ecx,5
L2:
mov eax,[edx]
mov [esi],eax
add esi, 4
add edx, 4
loop L2

I'm having problems adding numbers in an array -- x86 MASM assembly

I have to create a random range of 100 counts of numbers from 27 to 58 and then add up all the numbers in the 100 positions for a total amount. However, when I do that I get a random number and ninety-nine 32's as a result. I've searched everywhere and tried possible solutions but I'm either getting the same result or random garbage. Can someone offer some help?
INCLUDE irvine32.inc
.data
a DWORD 27
b DWORD 58
delta DWORD ?
source DWORD 100 DUP(?)
prompt BYTE "The sum of the 100 counts in array is ",0
.code
main PROC
Call Randomize
mov edi, 0
mov edi, OFFSET delta
mov esi, OFFSET source
mov eax, b
sub eax, a
inc eax
mov delta, eax
mov ecx, LENGTHOF source
mov eax, 0
L1:
mov eax, delta
call randomrange
add eax, a
mov source, eax
call writedec
mov al, " "
call writechar
loop L1
call crlf
call crlf
mov ecx, SIZEOF source
mov edx, OFFSET prompt
call writestring
l2:
add eax,[esi]
add esi, TYPE delta
call writedec
mov al, " "
call writechar
loop l2
exit
main ENDP
END main
I assume randomrange leaves its random number in EAX.
In the L1 loop, you add A to EAX to get your random value and then copy it to the first element of SOURCE every time through the L1 loop. That's why the first element is random, but the rest of the array isn't touched. (Note that you have the same problem in L2 -- you always get the value to print from the first element of SOURCE.)

Resources