Understanding Variables, Constants, and Arrays in Assembly Programming - Prof. Professor, Study notes of Microprocessor and Assembly Language Programming

An introduction to variables, constants, and arrays in assembly programming. It covers the concept of variables as memory locations, their declaration using DB and DW directives, the difference between BYTE and WORD variables, and the use of constants using the EQU directive. The document also explains how to get the address of a variable using LEA instruction or OFFSET operator, and the concept of arrays as chains of variables. Examples are given to illustrate the concepts.

Typology: Study notes

2021/2022

Uploaded on 02/23/2022

ujala-jamil
ujala-jamil 🇵🇰

5

(1)

4 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab No.5
Student Name: Roll no:
To learn the concept of Variables, Constants and understanding the concept
of Arrays using Variables & Constants
Objective
In this lab you will become:
Familiar with variables:
Familiar with address of variable:
Familiar with Offset & LEA:
Familiar with Constants
Familiar with how to check constant in numbers:
Familiar with Arrays
Variables
Variable is a memory location. For a programmer it is much easier to have some value be kept in
a variable named "var1" then at the address 5A73:235B, especially when you have 10 or more
variables.
Our compiler supports two types of variables: BYTE and WORD.
Syntax for a variable declaration:
name DB value
name DW value
DB - stays for Define Byte.
DW - stays for Define Word.
name - can be any letter or digit combination, though it should start with a letter. It's possible
to declare unnamed variables by not specifying the name (this variable will have an address
but no name).
value - can be any numeric value in any supported numbering system (hexadecimal, binary,
or decimal), or "?" symbol for variables that are not initialized.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Understanding Variables, Constants, and Arrays in Assembly Programming - Prof. Professor and more Study notes Microprocessor and Assembly Language Programming in PDF only on Docsity!

Lab No.

Student Name: Roll no:

To learn the concept of Variables, Constants and understanding the concept

of Arrays using Variables & Constants

Objective

In this lab you will become:  Familiar with variables:  Familiar with address of variable:  Familiar with Offset & LEA:  Familiar with Constants  Familiar with how to check constant in numbers:  Familiar with Arrays

Variables

Variable is a memory location. For a programmer it is much easier to have some value be kept in a variable named " var1 " then at the address 5A73:235B, especially when you have 10 or more variables. Our compiler supports two types of variables: BYTE and WORD. Syntax for a variable declaration: name DB value name DW value DB - stays for Define Byte. DW - stays for Define Word. name - can be any letter or digit combination, though it should start with a letter. It's possible to declare unnamed variables by not specifying the name (this variable will have an address but no name). value - can be any numeric value in any supported numbering system (hexadecimal, binary, or decimal), or "? " symbol for variables that are not initialized.

Example: ORG 100h MOV AL, var MOV BX, var RET ; stops the program. VAR1 DB 7 var2 DW 1234h As you see this looks a lot like our example, except that variables are replaced with actual memory locations. When compiler makes machine code, it automatically replaces all variable names with their offsets. By default segment is loaded in DS register (when COM files is loaded the value of DS register is set to the same value as CS register - code segment). In memory list first row is an offset , second row is a hexadecimal value , third row is decimal value , and last row is an ASCII character value. Compiler is not case sensitive, so " VAR1 " and " var1 " refer to the same variable. The offset of VAR1 is 0108h , and full address is 0B56:. The offset of var2 is 0109h , and full address is 0B56:0109 , this variable is a WORD so it occupies 2 BYTES. It is assumed that low byte is stored at lower address, so 34h is located before 12h.

There is LEA (Load Effective Address) instruction and alternative OFFSET operator. Both OFFSET and LEA can be used to get the offset address of the variable. LEA is more powerful because it also allows you to get the address of an indexed variable. Getting the address of the variable can be very useful in some situations, for example when you need to pass parameters to a procedure. Reminder: In order to tell the compiler about data type, these prefixes should be used: BYTE PTR - for byte. WORD PTR - for word (two bytes). For example: BYTE PTR [BX] ; byte access. or WORD PTR [BX] ; word access. assembler supports shorter prefixes as well: b. - for BYTE PTR w. - for WORD PTR in certain cases the assembler can calculate the data type automatically.

Code:

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Both examples have the same functionality. These lines: LEA BX, VAR MOV BX, OFFSET VAR are even compiled into the same machine code: MOV BX, num

Constants

Constants are just like variables, but they exist only until your program is compiled (assembled). After definition of a constant its value cannot be changed. To define constants EQU directive is used: name EQU < any expression > For example: k EQU 5 MOV AX, k The above example is functionally identical to code: MOV AX, 5 To view arrays you should click on a variable and set Elements property to array size. In assembly language there are not strict data types, so any variable can be presented as an array. Variable can be viewed in any numbering system:  HEX - hexadecimal (base 16).  BIN - binary (base 2).  OCT - octal (base 8).  SIGNED - signed decimal (base 10).  UNSIGNED - unsigned decimal (base 10).  CHAR - ASCII char code (there are 256 symbols, some symbols are invisible). You can edit a variable's value when your program is running, simply double click it, or select it and click Edit button. It is possible to enter numbers in any system, hexadecimal numbers should have " h " suffix, binary " b " suffix, octal " o " suffix, decimal numbers require no suffix. String can be entered this way:

number DUP ( value(s) ) number - number of duplicate to make (any constant value). value - expression that DUP will duplicate. for example: c DB 5 DUP(9) is an alternative way of declaring: c DB 9, 9, 9, 9, 9 one more example: d DB 5 DUP(1, 2) is an alternative way of declaring: d DB 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 Of course, you can use DW instead of DB if it's required to keep values larger then 255, or smaller then -128. DW cannot be used to declare strings. Code: An Assembly Program, which should add two 5-byte numbers (numbers are stored in array- NUM1 & NUM2), and stores the sum in another array named RESULT

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

Conclusion:

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

__________________________________________________________________

**Question no.1: Define variable with the help of example





Question no.2: Differentiate between variable and constant?






Question no.3: Define Array





______________________________________________________________________________**