CS130: Computer Architecture Project 9 (PJ 9) Dr. Lin Version 9/24/21 CS225

CS130: Computer Architecture Project 9 (PJ 9) Dr. Lin Version 9/24/21

CS225 Fundamentals of Computer Science Course Syllabus Fall 2013

Dr. Simon Lin Version: 09/02/2013

PJ 9 – Array Game of Numbers

Please follow the instructions to complete this project assignment.

(1) Go to Internet: http://www.jblearning.com/catalog/9781284036121/

(2) Click Sample Materials in the middle of the panel.

(3) Click Visual Studio 2015 Files to download VS_2015_Introduction_files.zip into your Downloads folder.

(4) Create a new folder Visual Studio Detmer in your USB drive.

(5) Click to open VS_2015_Introduction_files.zip (in your Downloads folder) to see all the contents. Copy all those folders and files (under the zip folder) into your USB drive folder: Visual Studio Detmer.

(6) Open your Visual Studio 2019. In Visual Studio, click File Open Project/Solution, and open your USB drive folder: Visual Studio Detmer. Double-click its sub-folder Windows32, and click windows32.sln, and click Open button. Now, you are running this windows32 application.

(7) Double-click the file example.asm (in Solution Explorer) to show its contents on the center panel.

(8) Right-click the file example.asm, and rename it to PJ9.asm.

(9) Modify PJ9.asm program as follows:

[a] The top 3 lines must be block comments to show Author, Date, and Purpose.

[b] Remove those blank lines from this program so that the complete program can be displayed on the

center panel. It may have only 32 lines or less now.

[c] This windows32 application will allow only one procedure with the name _MainProc. To run this

program, you need to use the name _MainProc for PROC, and use different PROC names for any

other assembly programs in this windows32 application. Please note that you may create many

assembly programs here (for testing), but only one of them with PROC being _MainProc can run.

Another way to solve this problem is to modify framework.c program so that this driver program

will call your proc (such as MainProcPJ9, instead of MainProc) directly.

Another way to solve this problem is to modify framework.c program so that this driver program

will call your proc (such as PJ9, instead of MainProc) directly.

(10) Write your PJ9.asm program as follows:

[a] create or declare an array of 6 double-word integers with or without initial values;

[b] prepare a loop to get 6 integers from the user, and store those 6 integers into the array accordingly;

[c] get and show those 6 integers (entered by the user) by using 6 message boxes;

[d] compute the average of those 6 integers, and show the average to the user using a message box;

[e] show the user each number that is smaller than the average using a message box. You must not add 10 to each number that is smaller than the average.

Note: The original example.asm program shows you how to prompt input from the user and how to display the result to the user using message boxes.

(11) Fully test this program to make sure it runs successfully with no errors.

(12) Copy and paste your complete PJ9.asm program into section (A) below.

(13) Build Rebuild Windows32 application. Click Local Windows Debugger to run this program.

(14) Make a screen print (by Ctrl+Alt+PrintScrn) and paste (Ctrl+V) it onto section (B) below.

(15) Make a screen print for each number [being displayed to the user] onto section (C) below.

==========================================================================.

Please reference the following 3 sample assembly code for this assignment.

Sample assembly code(1) to process an array using indexed addressing: (reference page 159-161)

; Given an array of doubleword integers, (1) find the average of those integers, and

; (2) add 10 to each number that is smaller than the average [using indexed addressing].

; But, you should not do this adding of 10 for this project assignment.

.586 ; 32-bits instructions

.MODEL FLAT ; Flat memory model for execution

.STACK 4096 ; Allocate 4K bytes STACK

.DATA ; Data declaration follows:

nbrArray DWORD 0, 1, 2, 3, 4, 5, 6 DUP (?) ; // Total 12 (= 6+6) integers in array.

; // First 6 numbers (0,1,2,3,4,5) followed by 6 uninitialized numbers. Total: 12 elements.

nbrElts DWORD 6 ; // 6 is the number of elements to be processed

.CODE

_MainProc PROC

; find sum and average

mov eax,0 ; sum := 0

mov ecx,0 ; index := 0 ; must start with 0, not 1

for1: cmp ecx,nbrElts ; index < nbrElts

jnl endFor1 ; exit loop if not less than

add eax,nbrArray[4*ecx] ; add number to sum in EAX ; times 4 since 4 bytes per number

inc ecx ; increment index ecx by 1

jmp for1 ; repeat for1 loop

endFor1:

cdq ; extend sum to quadword

idiv nbrElts ; divide by nbrElts to calculate average of those numbers

; add 10 to each array element below average

mov ecx,0 ; index := 0 to start with element 0

for2: cmp ecx,nbrElts ; index < nbrElts

jnl endFor2 ; exit if not

cmp nbrArray[4*ecx],eax ; number < average ?

jnl endIfSmall ; continue if not less

add DWORD PTR nbrArray[4*ecx], 10 ; add 10 to number

endIfSmall:

inc ecx ; increment index

jmp for2 ; repeat

endFor2:

quit: mov eax, 0 ; exit with return code 0

ret

_MainProc ENDP

END

==========================================================================.

Sample assembly code(2) to show that how this program should be written to avoid any damage to the sum value. EAX register cannot be used to store the sum since EAX is used for each atod conversion. Therefore, EDX register must be used to hold the sum.

.DATA

nbrArray DWORD 6 DUP (?) ; // Total 6 integers with no initial values

nbrElts DWORD 6 ; 6 integers to prompt from user ; number of elements

number1 DWORD ? ; number1 is double-word of 32 bits with no initial value

prompt1 BYTE “Enter the next number”, 0 ; prompt1 is a string of 18 bytes

string BYTE 40 DUP (?) ; string has 40 bytes with no initial value

outLabel2 BYTE “The number < average is", 0 ; a string of some bytes ended by 0.

outLabel3 BYTE “The average is”, 0 ; a string of some bytes ended by 0.

result BYTE 11 DUP (?), 0 ; a string of 11 bytes with no initial value

.CODE

_MainProc PROC

; find sum and average

mov eax,0 ; used for atod conversion and idiv operation

mov EDX,0 ; EDX to accumulate the sum, cannot use EAX

mov ecx,0 ; index := 0 ; 32-bit count register

for1: cmp ecx,nbrElts ; index < nbrElts

jnl endFor1 ; exit if not less

input prompt1, string, 40 ; prompt a number and put it in string of 40 bytes

atod string ; convert string to double-word integer, & put it into EAX

mov number1, EAX ; move EAX (containing a number) to number1

mov nbrArray[4*ecx], EAX ; move the number in EAX to the array at ecx postition

; times 4 because each integer takes 4 bytes in array

add EDX ,nbrArray[4*ecx] ; add the number in array to the sum in EDX

inc ecx ; increment index for next element

jmp for1 ; repeat for1 loop

endFor1: ; end of for1 loop

==========================================================================.

Sample assembly code(3) to (a) compute the average, and (b) show each number that is less than the average.

mov EAX, EDX ; move the sum in EDX to EAX

cdq ; extend sum_EAX to quadword

idiv nbrElts ; calculate average; divide EAX by nbrElts with quotient into EAX

dtoa result, EAX ; convert EAX value to ASCII characters for display

output outLabel3 , result ; output label and result which is the average

; show each element which is < average that is in EAX

mov ecx,0 ; index := 0 ; ecx is 32-bit count register.

for2: cmp ecx,nbrElts ; index < nbrElts

jnl endFor2 ; exit if ecx > nbrElts ; not less means >

cmp nbrArray[4*ecx],eax ; compare number < average_in_eax ?

jnl endIfSmall ; jump if number >= average ; not less means >=

; Now, the number is < average, so output it

dtoa result, nbrArray[4*ecx] ; convert this number to ASCII for output

output outLabel2 , result ; output label and result

endIfSmall:

inc ecx ; increment index for next element

jmp for2 ; repeat for2 loop

endFor2:

==========================================================================.

How to submit your PJ 9?

You must copy/paste (or Ctrl+Alt+PrintScrn/paste) your programs/screens to all the sections below.

Submit this document (for example: CS130-PJ9.docx) through the Canvas system (https://ilearn.laccd.edu ).

==========================================================================.

//You must delete everything above & including this line to make this your Word document to be submitted.

CS130 PJ 9 Report My Name:____________________

(A) The following is my source program: PJ9.asm. You must copy/paste your source code from your Visual Studio. As you may see, the font of the program in Visual Studio is Consolas, not Courier New or Times New Roman. You must not show screen prints here.

(B) The following is the screen print of my Visual Studio Console after the program PJ9.asm has been run successfully with return code 0: (You must Ctrl+Alt+PrintScrn your Visual Studio Console, and then paste it to here.)

(C) The following is the screen print of my output for each message box being displayed to the user. (You must Ctrl+Alt+PrintScrn your Visual Studio Console, and then paste it to here.)

Sample Test for you to verify your program:

Input 6 numbers: 1, 2, 3, 4, 5, 6

Display average: 3

Display all numbers below average: 1, 2

[C1] Show to get the 1st number.

[C2] Show to get the 2nd number.

[C3] Show to get the 3rd number.

[C4] Show to get the 4th number.

[C5] Show to get the 5th number.

[C6] Show to get the 6th number.

[C7] Show the average of those 6 numbers.

[C8] Show the 1st number that is smaller than the average.

[C9] Show the 2nd number that is smaller than the average.

[C10] Show the 3rd number that is smaller than the average. (if any)

[C11] Show the 4th number that is smaller than the average. (if any)

Page 2 of 5

Page 1 of 2