0% found this document useful (0 votes)
76 views8 pages

CS 271 Lecture #20: Another Array Example

This document discusses displaying arrays sequentially in assembly language. It provides two versions of a display procedure that loops through an array and prints out the elements. The first version uses register indirect addressing to access elements, while the second uses base-indexed addressing. The document also covers generating random numbers using the Irvine library procedures Randomize and RandomRange. It demonstrates how to get a random integer within a specified range using these procedures. Finally, it briefly mentions passing arrays by reference.

Uploaded by

Isak Foshay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views8 pages

CS 271 Lecture #20: Another Array Example

This document discusses displaying arrays sequentially in assembly language. It provides two versions of a display procedure that loops through an array and prints out the elements. The first version uses register indirect addressing to access elements, while the second uses base-indexed addressing. The document also covers generating random numbers using the Irvine library procedures Randomize and RandomRange. It demonstrates how to get a random integer within a specified range using these procedures. Finally, it briefly mentions passing arrays by reference.

Uploaded by

Isak Foshay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CS 271 Lecture #20

Another Array Example


Display an Array Sequentially

“Random” Numbers
Setup in calling procedure

.data
list DWORD 100 DUP(?)
count DWORD 0

.code
;...
;code to initialize list and count
;...
;set up parameters and call display
push OFFSET list ;@list
push count ;number of elements
call display
;...
Display: version 0.1 (register indirect)
display PROC
push ebp
mov ebp,esp
mov esi,[ebp+12] ;@list
mov ecx,[ebp+8] ;ecx is loop control
more:
mov eax,[esi] ;get current element
call WriteDec
call Crlf
add esi,4 ;next element
loop more
endMore:
pop ebp
ret 8
display ENDP
Display: version 0.2 (base-indexed)
display PROC
push ebp
mov ebp,esp
mov esi,[ebp+12] ;@list
mov ecx,[ebp+8] ;ecx is loop control
mov edx,0 ;edx is element “pointer”
more:
mov eax,[esi+edx] ;get current element
call WriteDec
call Crlf
add edx,4 ;next element
loop more
endMore:
pop ebp
ret 8
display ENDP
Random Numbers

• Irvine library has random integer generator


• "pseudo-random" numbers
• Randomize procedure
• Initializes sequence based on system clock (random
seed)
• Call once at the beginning of the program
• Without Randomize, program gets the same
sequence every time it is executed.
Limiting random values

• RandomRange procedure
• Accepts N > 0 in eax
• Returns random integer in [0 .. N-1] in eax
• To generate a random number in [lo .. hi]:
• Find number of integers possible in [lo .. hi] :
range = hi – lo +1
• Put range in eax, and call RandomRange
• Result in eax is in [0 .. range -1]
• Add lo to eax.
RandomRange Example

• Get a random integer in range [18 .. 31]

mov eax,hi ;31


sub eax,lo ;31-18 = 13
inc eax ;14
call RandomRange ;eax in [0..13]
add eax,lo ;eax in [18..31]
Passing arrays by reference

• See demo5.asm

• Do Programming Project #5
• OK to borrow code from these slides
• Always cite reference for borrowed code

You might also like