Chapter 6
Conditional Processing
Assembly Language Programming Exercise
Problem # 1:
Create a procedure that fills an array of doublewords with N random integers, making sure the values fall within the range j...k, inclusive. When calling the procedure, pass a pointer to the array that will hold the data, pass N, and pass the values of j and k. Preserve all register values between calls to the procedure. Write a test program that calls the procedure twice, using different values for j and k. Verify your results using a debugger.
Solution:
INCLUDE Irvine32.inc
N=10
.data
array DWORD N DUP(?)
j DWORD ?
k DWORD ?
.code
main PROC
call Clrscr
mov j, -10
mov k, 10
mov ESI, OFFSET array
mov ECX, N
call FillingAnArray
mov j, 100
mov k, 1000
mov ESI, OFFSET array
mov ECX, N
call FillingAnArray
call WaitMsg
exit
main ENDP
FillingAnArray PROC
push ecx
push esi
l1:
mov eax, j
mov ebx, k
dec ebx
sub ebx, eax ;create range from 0 to N
xchg ebx, eax ;random works with eax
call RandomRange ; generate random int witin range 0 to N
neg ebx ;change sign of ebx
sub eax, ebx ;sub from eax to define range
call crlf
call WriteInt
mov [esi], eax
add esi, 4
loop l1
pop esi
pop ecx
ret
FillingAnArray ENDP
END main
N=10
.data
array DWORD N DUP(?)
j DWORD ?
k DWORD ?
.code
main PROC
call Clrscr
mov j, -10
mov k, 10
mov ESI, OFFSET array
mov ECX, N
call FillingAnArray
mov j, 100
mov k, 1000
mov ESI, OFFSET array
mov ECX, N
call FillingAnArray
call WaitMsg
exit
main ENDP
FillingAnArray PROC
push ecx
push esi
l1:
mov eax, j
mov ebx, k
dec ebx
sub ebx, eax ;create range from 0 to N
xchg ebx, eax ;random works with eax
call RandomRange ; generate random int witin range 0 to N
neg ebx ;change sign of ebx
sub eax, ebx ;sub from eax to define range
call crlf
call WriteInt
mov [esi], eax
add esi, 4
loop l1
pop esi
pop ecx
ret
FillingAnArray ENDP
END main
Let me know in the comment section if you have any question.
Previous Post:
11. Program to Finding Multiples of K in Assembly Language using Visual Studio
Thank you a ton!
ReplyDelete