LAB 3 Memory Addresses Objective: To access and explain the computer memory and memory addresses. To develop a program for moving data between CPU and RAM, and find the contents and addresses for each memory variable. To exam the address relationship among the memory variables and declared poers. To find the size of a variable in bytes. To exam indirect memory addressing mode. 1) Type and explain Example 1. Compile, Build, and Run the program. Example 1 illustrates to use of a. a poer to store the address of each element in an 8-bit array b. the address of operator (&) to initialize a poer c. MOV instruction to move the address of each element in an array to registers EAX (or EBX or ECX or EDX, etc), such as MOV EAX, cptr d. sizeof operator to find out the number of bytes of each element in the array and the address of each element in the array. e. indirect memory addressing mode to access the content stored in the memory location, for instance, MOV DL, [EAX] 2) Explain each line of Example 1 (addressconten.cpp) and describe the output of each in your lab report 3) Modify Example 2 and Example 3 provided in addresscontentemp.cpp to do: a. find the content and memory address of a16-bit array b. find the content and memory address of a 32-bit array c. find the size in bytes of the content and memory address of a 16 bit array d. find the size in bytes of the content and memory address of a 32 bit array e. use indirect addressing mode to find the content stored in the memory location and compare to the content of the original array 4) Explore the relationship among these addresses in memory. Understand the addresses for a byte, a word, and a double word, respectively. 5) Draw a table of the relationship among the variable value, the size of the variable, and the memory address of the variable. 6) Analyze the information in the table to understand the Little Endian Computer 7) Exam byte-addressable memory to store a byte, a word, and a double word, respectively. Written exercises: A title page with the lab title, your name and other identification, the current date, and the due date. If you modify or create a program in an exercise, the source code with comments for that program should appear in the laboratory report. The output from all programs should also appear in the lab report. For each exercise, you should provide a write-up describing the purpose of the exercise, what you learned from the exercise, and any comments, improvements, or other work you ve done with the exercise. If you make any mistakes that require correction, you should include the source code for the incorrect program with your lab report. Hand write on the listing where the error occurs and describe what you did to correct the problem. Appropriate diagrams can be included if needed.
Example 1 /*********************************************************** File name: addresscontent.cpp Find the content and the address for each memory variable in the array Method: Embedding an in-line assembly language module in a C program A. Find the contents for each element of an 8-bit array B. Find the addresses for each element of an 8-bit array C. Find out the number of bytes of each element in an 8-bit array D. Find out the number of bytes of the address of each element in an 8-bit array E. Access the content stored in the memory location by indirect memory addressing mode MOV DL, [EAX] to access the content stored in the memory location EAX sizeof() to find the number of byte prf() is used to display information on the console scanf_s() is used to receive the information from the keyboard std::cout is used to display the output on the console system ("pause") is used to hold console *************************************************************/ #include <stdio.h> // to use prf() #include <iostream> // to use system ("pause") to hold console; main() /*An 8-bit one dimensional sc8_arr[5] array declaration and initialization. Poers of scptr0, scptr1, etc will be used to store the address of each element of the array A 32-bit array md[5] will be used to store each of poers A 32-bit array madd[5] is used to store the address of each element of the array A 8-bit array var[5] is used to store the retrieved value by indirect memory addressing mode*/ sc8_arr[5] = 0x41, 0x42, 0x79, 0x7A, 0x7B ; *scptr0, *scptr1, *scptr2, *scptr3, *scptr4; md[5], madd[5]; var[4]; i; // used as index //address for each element in an array sc8_arr[5] scptr0 = &sc8_arr [0]; scptr1 = &sc8_arr [1]; scptr2 = & sc8_arr[2]; scptr3 = & sc8_arr[3]; scptr4 = & sc8_arr[4]; //Display Hex value, decimal value, and value for each element of array prf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); prf("---------------the value of each element of 8-bit array-------\n"); for (i=0; i < 5; i++) prf ("The value of element %d in an array is 0x%x (hex), %d(decimal), %c (acter)\n", i, sc8_arr[i], sc8_arr[i], sc8_arr[i]);
//Display address in hexadecimal for each element of array prf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); prf("--------------the address of each element of 8-bit array-------\n"); for (i=0; i < 5; i++) prf ("The memory address of element %d in an array is 0x%X (hexidecimal)\n", i, scptr0+i); prf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); //find the total numbers in byte of array sc8_arr[5] // find the number of byte of each element of array prf("\n"); prf("--------------the size information in bytes of an 8-bit array-------\n"); prf ("The total numbers in bytes of an 8-bit array with 5 elements is %d bytes\n", sizeof (sc8_arr)); md[i] = sizeof (sc8_arr [i]); madd[i] = sizeof(scptr0+i); prf ("-------------------------------------------------------\n"); prf ("the size of element %d is %d bytes\n", i, md[i]); prf ("the size of the address of the element %d is %d bytes\n ", i, madd[i]); //Indirect memory addressing by MOV DL, [EAX] /*move the address of element 0 to EAX store the content at the address shown in EAX o DL(indirect addressing) move the content of DL o the variable of var[0]*/ MOV EAX, scptr0; MOV DL, [EAX]; MOV var[0], DL; /*move the address of element 1 to EAX store the content at the address shown in EAX o DL(indirect addressing) move the content of DL o the variable of var[1]*/ MOV EAX, scptr1; MOV DL, [EAX]; MOV var[1], DL; /* move the address of element 2 to EAX store the content at the address shown in EAX o DL(indirect addressing) move the content of DL o the variable of var[2]*/ MOV EAX, scptr2;
MOV DL, [EAX]; MOV var[2], DL; /*move the address of element 3 to EAX store the content at the address shown in EAX o DL(indirect addressing) move the content of DL o the variable of var[3]*/ MOV EAX, scptr3 MOV DL, [EAX]; MOV var[3], DL; prf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); prf("---------------the value of each element of two 8-bit arrays-------\n"); prf("the values at the index %d of the original array and the retrieved array \n", i); prf("by indirect addressing mode are: 0x%x(hex) and 0x%x(hex), respectively\n", sc8_arr[i], var[i]); prf ("-------------------------------------------------------\n"); system ("pause"); return 0;
The output from addresscontent.cpp Example 2 (Template for 16 bit array) /*********************************************************** File name: addresscontenttemp.cpp Find the content and the address for each memory variable in the array Method: Embedding an in-line assembly language module in a C program A. Find the contents for each element of a 16-bit array B. Find the addresses for each element of a 16-bit array C. Find out the number of bytes of each element in a 16-bit array D. Find out the number of bytes of the address of each element in a 16-bit array E. Access the content stored in the memory location by indirect memory addressing mode
MOV DL, [EAX] to access the content stored in the memory location EAX sizeof() to find the number of byte prf() is used to display information on the console scanf_s() is used to receive the information from the keyboard std::cout is used to display the output on the console system ("pause") is used to hold console *************************************************************/ #include <stdio.h> // to use prf() #include <iostream> // to use system ("pause"); main() /*A 16-bit one dimensional array declaration and initialization. Poers of svptr0, svptr1, etc will be used to store the address of each element of the array A 32-bit array md[4] will be used to store each of poers A 32-bit array madd[4] is used to store the address of each element of the array A 16-bit array svar[4] is used to store the retrived value by indirect memory addressing mode*/ short sh16_arr[4] = -1, -3, 1000, 2000; //16-bit array short *svptr0, *svptr1, *svptr2, *svptr3; // used to store the address md[4], madd[4]; short svar[4]; i; // used as index //address for each element in an array sh16_arr[4] //Display Hex value, decimal value, and short value for each element of array prf("---------------the value of each element of 16-bit array-------\n"); //Display address in hexadecimal for each element of array prf("\n"); prf("--------------the address of each element of 16-bit array-------\n"); // find the numbber of byte of each element of array prf("\n"); prf("--------------the size information in byte of 8-bit array-------\n"); //Indirect memory addressing by MOV DX, [EAX]
prf("---------------the value of each element of two 16-bit arrays-------\n"); system ("pause"); return 0; Example 3 (Template for 32 bit array) /*********************************************************** File name: addresscontenttemp.cpp Find the content and the address for each memory variable in the array Method: Embedding an in-line assembly language module in a C program A. Find the contents for each element of a 32-bit array B. Find the addresses for each element of a 32-bit array C. Find out the number of bytes of each element in a 32-bit array D. Find out the number of bytes of the address of each element in a 32-bit array E. Access the content stored in the memory location by indirect memory addressing mode MOV DL, [EAX] to access the content stored in the memory location EAX sizeof() to find the number of byte prf() is used to display information on the console scanf_s() is used to receive the information from the keyboard std::cout is used to display the output on the console system ("pause") is used to hold console *************************************************************/ #include <stdio.h> // to use prf() #include <iostream> // to use system ("pause"); main() /*A 32-bit one dimentional array declaration and initilization.
Poers of ivptr0, ivptr1, etc will be used to store the address of each element of the array A 32-bit array md[4] will be used to store each of poers A 32-bit array madd[4] is used to store the address of each element of the array A 32-bit array ivar[4] is used to store the retrived value by indirect memory addressing mode*/ i32_arr[4]=0x8000000, 0x0000ffff, -660000, 6600000; //32-bit (double word) array *ivptr0, *ivptr1, *ivptr2, *ivptr3; //ivptr0, ivptr1, ivptr2 are used to store the address md[4], madd[4]; short svar[4]; i; // used as index //address for each element in an array i32_arr[4] //Display Hex value, decimal value, and short value for each element of array prf("---------------the value of each element of 32-bit array-------\n"); //Display address in hexadecimal for each element of array prf("--------------the address of each element of 32-bit array-------\n"); // find the numbber of byte of each element of array prf("--------------the size information in byte of 8-bit array-------\n"); //Indirect memory addressing by MOV EDX, [EAX]
prf("---------------the value of each element of two 32-bit arrays-------\n"); system ("pause"); return 0; Fill in the table with data types, memory contents, and memory addresses Data type short short Short (array with 4 elements) short Variable name sc8_arr[0] sc8_arr[1] sc8_arr[2] sc8_arr[3] sc8_arr[4] sh16_arr[0] sh16_arr[1] sh16_arr[2] sh16_arr[3] i32_arr[0] i32_arr[1] i32_arr[2] i32_arr[3] Hexadecimal Decimal Address
Fill in the table with the size of memory contents, and the size of memory addresses, respectively Data type (array with 5 elements) short short short short (array with 4 elements) Variable name sc8_arr[0] sc8_arr[1] sc8_arr[2] sc8_arr[3] sc8_arr[4] sh16_arr[0] sh16_arr[1] sh16_arr[2] sh16_arr[3] i32_arr[0] i32_arr[1] i32_arr[2] i32_arr[3] Size of each contents in byte(s) Size of each memory addresses in byte(s)