FUNDAMENTALS OF PROGRAMMING:

A microprocessor is an integrated circuit that serves as the central processing unit (CPU) of a computer. It is a programmable device that can perform arithmetic and logic operations on data and execute instructions stored in memory. To communicate with the microprocessor, programmers use two types of languages: machine language and assembly language.

Machine language

Machine language is the lowest level programming language that the microprocessor understands. It is represented by a series of binary codes (0s and 1s) that are executed directly by the microprocessor. Each instruction in machine language corresponds to a specific operation that the microprocessor can perform, such as adding two numbers or moving data between memory and a register. Machine language is difficult to read and write, as it consists of long strings of binary code that are not easy for humans to understand.

Assembly language

Assembly language is a more human-readable version of machine language. It uses symbolic representations of the binary codes that the microprocessor understands. Each symbolic instruction in assembly language corresponds to a specific machine language instruction, and it is translated into binary code by an assembler program. Assembly language is easier to read and write than machine language, as it uses words and symbols that are easier for humans to understand. However, it still requires a strong understanding of the microprocessor’s architecture and instruction set.

Assembly language statements typically have four fields, which are also referred to as sections or parts. These fields are:

fundamentals-of-programming

  1. Label field: This field is optional and is used to define a name for the instruction or data. Labels are often used to mark the beginning of a program or a specific section of code.
  2. Operation field: This field contains the mnemonic instruction that represents the operation to be performed by the processor, such as ADD, SUB, JMP, etc.
  3. Operand field: This field specifies the data or memory location on which the operation is to be performed. The operand can be a constant, a register, a memory location, or a label.
  4. Comment field: This field is also optional and is used to add comments or remarks about the instruction or the code. Comments are ignored by the assembler and do not affect the program execution.

Here is an example of an assembly language statement with all four fields:

START: LDA NUM ; Load the value at memory location NUM into the accumulator.

ADD SUM ; Add the value at memory location SUM to the accumulator.       
STA RESULT ; Store the result in memory location RESULT

HLT ; Halt the program

;This program calculates the sum of two numbers stored in memory

In this example, the first field “START” is a label, the second field “LDA” is the mnemonic instruction, the third field “NUM” is the operand, and the fourth field “; Load the value at memory location NUM into the accumulator” is a comment.

Assemblers

An assembler is a software program that converts assembly language code into machine language code that can be executed by a microprocessor. Assemblers are essential tools in the process of developing software for microprocessors and other computing devices.

When an assembler receives an assembly language program, it first checks the syntax and structure of the program for errors. Then, it translates each assembly language instruction into the corresponding machine language instruction that the microprocessor can execute. The assembler generates an object file, which contains the machine language code, as well as other information that is needed to link the program with other modules and libraries.

Assemblers can be classified into two types: one-pass assemblers and multi-pass assemblers. One-pass assemblers scan the assembly language code only once and generate machine language code as they go. They are suitable for small programs and are relatively fast. Multi-pass assemblers, on the other hand, make multiple passes over the assembly language code, resolving symbols and generating machine code in separate steps. They are more efficient for larger programs but may be slower.

Assemblers also support a variety of directives that provide additional information to the assembler, such as the location of memory addresses and the allocation of memory space for data. These directives help the assembler generate machine code that correctly references memory locations and data.

Overall, assemblers are essential tools for converting assembly language code into machine language code that can be executed by microprocessors. They help developers write efficient and reliable software for a wide range of computing devices.

Each assembler has its own unique pseudo instruction written in assembly language format.

A pseudo instruction is an instruction in assembly language that does not correspond directly to a machine language instruction but instead provides additional functionality to the assembler or simplifies the task of programming. Pseudo instructions are also known as assembler directives or macro instructions.

Pseudo instructions are often used to define constants, reserve memory space, or include external files in the assembly language program. They are not executed by the microprocessor, but instead are processed by the assembler during the assembly process.

Here are some examples of commonly used assembler directives (also known as pseudo instructions) in assembly language programming:

  1. ORG: This directive is used to specify the starting address of the program or a section of the program. For example, the following code sets the starting address of the program to 0x1000:
ORG 0x1000
  1. EQU: This directive is used to define a symbol as a constant value that is used throughout the program. For example, the following code defines the symbol “MYCONST” as the value 42:
MYCONST EQU 42
  1. DB: This directive is used to allocate one or more bytes of memory and initialize them with data values. For example, the following code allocates four bytes of memory and initializes them with the values 1, 2, 3, and 4:
MYDATA DB 1, 2, 3, 4
  1. DW: This directive is used to allocate one or more words (two bytes each) of memory and initialize them with data values. For example, the following code allocates two words of memory and initializes them with the values 0x1234 and 0x5678:
MYDATA DW 0x1234, 0x5678
  1. DS: This directive is used to reserve a block of memory of a specified size. For example, the following code reserves 256 bytes of memory:
MYMEM DS 256
  1. END: This directive marks the end of the program. For example:
END
  1. INCLUDE: This directive is used to include an external file in the assembly language program. For example, the following code includes the file “MYLIB.ASM” in the current program:
INCLUDE MYLIB.ASM
  1. IF: This directive is used to conditionally assemble code based on a given condition. For example:
IF MYFLAG == 1
; assemble code here if MYFLAG is 1
ENDIF
  1. ELSE: This directive is used in conjunction with the IF directive to specify code to assemble when the condition is false. For example:
IF MYFLAG == 1
; assemble code here if MYFLAG is 1
ELSE
; assemble code here if MYFLAG is not 1
ENDIF
  1. ENDIF: This directive is used to mark the end of the IF-ELSE block. For example:
IF MYFLAG == 1
; assemble code here if MYFLAG is 1
ELSE
; assemble code here if MYFLAG is not 1
ENDIF
  1. MACRO: This directive is used to define a macro instruction that can be used to simplify the task of programming by encapsulating a set of instructions. For example:
MYMACRO MACRO ARG1, ARG2
; macro code here
ENDM
  1. ENDM: This directive marks the end of a macro definition. For example:
MYMACRO MACRO ARG1, ARG2
; macro code here
ENDM

These are just a few examples of the many assembler directives available in assembly language programming. They can be very useful for defining constants, reserving memory space, and simplifying programming tasks.

Macros:

In assembly language programming, a macro is a set of instructions that can be defined once and used multiple times throughout the program. Macros can be very useful for simplifying the task of programming by encapsulating a set of instructions into a single instruction that can be used in multiple places in the program. This can help to reduce code duplication and improve code readability.

Here is an example of a macro definition in assembly language:

Sometimes it is required that same set of instructions are to be repeated again & again. One way to simplify the problem is the care of subroutine. The other way is the use of macros. The assemblers which have the capability to process macro instructions are called macro assemblers. The assemblers are designed such that the programmer need to write set of instruction once and then refer it many times as desired.

A macro instruction is a single instruction that the macro assemble replaces with a group of instruction whenever it applies in an assembly language program. The macro instruction and the instruction that replace it are defined by the system design only once in the program. Macros are useful when a small group of instruction must be repeated several times in a program, with only minor or no changes in each repetition.

MYMACRO MACRO ARG1, ARG2
MOV AX, ARG1
ADD AX, ARG2
MOV RESULT, AX
ENDM

In this example, the macro is called “MYMACRO” and takes two arguments, “ARG1” and “ARG2”. The macro consists of three instructions: move the value of “ARG1” into the AX register, add the value of “ARG2” to the AX register, and move the result into the “RESULT” variable. The “RESULT” variable would need to be defined somewhere else in the program.

Once the macro is defined, it can be used throughout the program like any other instruction. Here is an example of how the macro might be used:

MYMACRO 10, 20 ; adds 10 and 20 and stores the result in RESULT

In this example, the macro is called with the arguments 10 and 20. The macro would then execute the three instructions defined in the macro definition, passing in the values of the arguments and storing the result in the “RESULT” variable.

Overall, macros can be a powerful tool for simplifying the task of assembly language programming by encapsulating a set of instructions into a single instruction that can be used multiple times throughout the program.

The use of macro in ALP entails three groups:

  1. Macro Definition: This group consists of the macro definition itself, which includes the instructions that make up the macro and any parameters that it takes. The macro definition is written once and can be used multiple times throughout the program.
  2. Macro Invocation: This group consists of the instructions that call the macro, passing in any necessary parameters. When the macro is invoked, the instructions in the macro definition are executed with the specified parameters.
  3. Macro Expansion: This group consists of the expanded instructions that result from the macro invocation. When the macro is invoked, the instructions in the macro definition are replaced with the expanded instructions that result from the macro expansion. These expanded instructions are then assembled into machine code like any other instruction.

By using macros in this way, assembly language programmers can simplify their code by defining complex instructions as macros, and then using these macros throughout their programs. Macros can help to reduce code duplication, improve code readability, and make it easier to modify and maintain the code over time.

The macro definition has the following format

MACRO macro_name parameter_list

; Macro instructions go here

; They can use the parameters passed in

ENDM

macro

In this format:

  • MACRO and ENDM are assembler directives that define the beginning and end of the macro definition, respectively.
  • macro_name is a name given to the macro. This is the name that will be used when the macro is invoked.
  • parameter_list is a comma-separated list of parameters that the macro can accept. These parameters are like variables that can be used in the macro instructions.

Within the macro definition, you can use any valid assembly language instructions and directives, as well as the parameters passed in through the parameter_list. When the macro is invoked, the assembler will replace the macro call with the macro instructions, replacing any parameter references with the actual values passed in.

For example, here is a simple macro definition that adds two numbers:

MACRO ADD_TWO num1, num2
MOV AX, num1
ADD AX, num2
MOV result, AX
ENDM

This macro takes two parameters, num1 and num2, and adds them together, storing the result in a variable called result. When the macro is invoked, the num1 and num2 parameters will be replaced with the actual values passed in, and the result variable will be replaced with the appropriate memory address. For example:

ADD_TWO 5, 10 ; expands to MOV AX, 5; ADD AX, 10; MOV result, AX

This macro call will add the numbers 5 and 10 together and store the result in the result variable.