Docsity

Microcontrollers and interfacing Lab work, Lab Reports of Microcontrollers

Toggle sidebar
bg1
Electrical Engineering Department – ITU
EE243L: Microcontrollers and Interfacing Lab
Course Instructor: Dated:
Lab Engineer 1: Semester:
Lab Engineer 2: Session:
Lab 2 – Data Definition in Memory and Addressing Modes in x86
Assembly Language
Name Roll number Report
(out of 25) Scaled to 10 Viva
(out of 5)
Total
(out of 15)
Muhammad Bilal Hassan bsee19024
Checked on: ____________________________
Signature: ____________________________
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Microcontrollers and interfacing Lab work and more Lab Reports Microcontrollers in PDF only on Docsity!

Electrical Engineering Department – ITU

EE243L: Microcontrollers and Interfacing Lab

Course Instructor: Dated: Lab Engineer 1: Semester: Lab Engineer 2: Session:

Lab 2 – Data Definition in Memory and Addressing Modes in x

Assembly Language

Name Roll number Report (out of 25) Scaled to 10 Viva (out of 5) Total (out of 15) Muhammad Bilal Hassan bsee Checked on: ____________________________ Signature: ____________________________

EE243L: Microcontrollers and Interfacing Lab

2 Data Definition in Memory and Addressing Modes

in x86 Assembly Language

Introduction

This lab introduces students to memory access in assembly language. Data definition in memory and transfer to CPU registers is studied. Students also learn how to initialize arrays in memory and manipulate array data. A brief introduction of the addressing modes in assembly language is also offered.

Lab goals

This lab will enable students to achieve the following:  Use memory by defining variables and arrays in memory  Learn how to perform arithmetic and logical operations on data in memory  Find size and length of arrays and manipulate data stored in arrays  Learn about the different addressing modes in x86 assembly language

Lab conduct

  1. You have to perform this experiment using Visual Studio 2015 and Irvine libraries for MASM.
  2. You are required to work individually; everyone must attempt to understand the purpose of assembly language coding and simulated set up for the given experiment.
  3. In case some aspect of the lab experiment is not understood, you are advised to seek help from the instructor, lab engineer or the TA.

Lab details

2.1 Data definition in memory

2.1.1 Data types and data definition statement [variable-name] define-directive initial-value The following is an example of a data definition statement: count DWORD 12345 Table 2. 1 : Intrinsic data types and their sizes in memory Type Usage BYTE 8-bit unsigned integer. B stands for byte SBYTE 8-bit signed integer. S stands for signed WORD 16-bit unsigned integer SWORD 16-bit signed integer DWORD 32-bit unsigned integer. D stands for double SDWORD 32-bit signed integer. SD stands for signed double TBYTE 80-bit (10 byte) integer REAL4 32-bit (4 byte) IEEE floating-point number REAL8 64-bit (8 byte) IEEE double-precision floating-point number REAL10 80-bit (10 byte) IEEE extended-precision floating-point number 2.1.2 Defining memory locations The following lines of code illustrate how to assign labels to memory: DATA SEGMENT MEMBDS DB?

EE243L: Microcontrollers and Interfacing Lab byte1 BYTE 10, 20, 30 array3 DWORD 1, 2, 3, 4 digitStr BYTE "12345678", 0 LENGTHOF byte1; returns 3 LENGTHOF array3; returns 4 LENGTHOF digitStr; returns 9

2.3 Addressing modes

The following table shows the different addressing modes of x86 microprocessor assembly language: Table 2. 2 : Addressing modes in x86 assembly language Addressing Mode Mnemonic Description Immediate MOV AX, 1000H Source of data is within instruction Register MOV DX, CX Source and destination of data are CPU registers Direct MOV AH, [MEMBDS] Memory address is supplied within the instruction Register indirect MOV AX, [SI] Memory address is supplied in an index or pointer register Indexed MOV AX, [SI + 6] Memory address is the sum of the index register plus a displacement within the instruction Based MOV AX, [BP + 2] Memory address is the sum of the BX or BP base registers plus a displacement within instruction Based and indexed MOV AX, [BX + SI] Memory address is the sum of an index register and a base register Based and indexed with displacement MOV AX, [BX + SI + 5]^ Memory address is the sum of an index register, a base register and a displacement within instruction 2.3.1 Indirect operands for memory access An indirect operand can be any 32-bit general-purpose register (EAX, EBX, ECX, EDX, ESI, EDI, EBP, and ESP) surrounded by brackets. The register is assumed to contain the address of some data. In the next example, ESI contains the offset of byteVal. The MOV instruction uses the indirect operand as the source, the offset in ESI is dereferenced, and a byte is moved to AL: .data byteVal BYTE 10h .code mov esi, OFFSET byteVal mov al, [esi]; AL = 10h If the destination operand uses indirect addressing, a new value is placed in memory at the location pointed to by the register. In the following example, the contents of the BL register are copied to the memory location addressed by ESI. mov [esi], bl Indirect operands are ideal tools for stepping through arrays. In the following example, arrayB contains 3 bytes. As ESI is incremented, it points to each byte, in order: .data arrayB BYTE 10h, 20h, 30h .code mov esi, OFFSET arrayB mov al, [esi]; AL = 10h inc esi mov al, [esi]; AL = 20h inc esi mov al, [esi]; AL = 30h

Lab- 2

Lab tasks

  1. Define a byte array in your code as: arrayB BYTE 10h, 20h, 30h. Add the three bytes in the array and save the result at the fourth place in arrayB after the last element 30h. Paste screenshot of your code and the Memory window at the end of the execution of your code, showing that the sum has been successfully placed at the fourth memory place:
  2. Define a double word array in your code as: arrayD DWORD 10h, 20h, 30h. Now add the three double words in this array and save the result at the fourth place in arrayD after the last element 30h. Note that, this time, a displacement of 4 must be added to ESI, or any other register that you are using to index, as it points to each subsequent array value because double words are 4 bytes long. Paste here the screenshots of code and Memory window showing the final result:

.data arrayB BYTE 10h, 20h, 30h .code main PROC mov AL, arrayB mov BL, [arrayB + 1] mov CL, [arrayB + 2] add AL, BL add AL, CL mov [arrayB + 3], AL INVOKE ExitProcess, main ENDP END main


------------------------------------------------------------------------------- ------- .data arrayD DWORD 10h, 20h, 30h .code main PROC mov EAX, arrayD mov EBX, [arrayD + 4] mov ECX, [arrayD + 8] add EAX, EBX add EAX, ECX mov [arrayD + 12], EAX INVOKE ExitProcess, main ENDP

Lab- 2

  1. Define an integer array as: arr byte 1, 2, 3, 4. Reverse the order of this array and store the reversed array at another array reverse_arr. Follow the following steps to carry out the order reversal:
    • Use a general-purpose register, say, EAX to load the address of the last array element.
    • Store the content at the address loaded in EAX to the first location of reverse_arr.
    • Decrement EAX.
    • Again store the content at the address in EAX to the second location of reverse_arr.
    • Repeat two more times. Paste here the screenshots of code and the final result as viewed in Registers and Memory windows: .data arr byte 1, 2, 3, 4 reverse byte 0,0,0, .code main PROC mov EAX, OFFSET [arr + 3] mov EBX , [EAX] mov reverse,BL dec EAX mov EBX , [EAX] mov [reverse + 1],BL dec EAX mov EBX , [EAX] mov [reverse + 2],BL dec EAX mov EBX , [EAX] mov [reverse + 3],BL INVOKE ExitProcess, main ENDP END main

EE243L: Microcontrollers and Interfacing Lab

  1. Identify the addressing modes being employed in each of the following statements: Statement Addressing mode MOV AH, 47H Immediate MOV BH, AH Register MOV AH, [XRAY] ; Suppose that XRAY is defined in memory Direct MOV AH, [XRAY + 1] Direct Indexed MOV BH, [SI] Register indirect MOV BH, [SI+1] Indirect indexed

EE243L: Microcontrollers and Interfacing Lab Signature: ________________________ Signature: ________________________ Date: ____________________________ Date: ____________________________