S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

16
SYSTEMS PROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury

Transcript of S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

Page 1: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

SYSTEMS PROGRAMMING

CHAPTER 2

PROGRAMMING IN ASSEMBLY LANGUAGE

Er. Bharadwaj Choudhury

Page 2: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

HIGH LEVEL LANGUAGE CONSTRUCTS

Constants: Numerical, string or other quantities whose unchanging actual values are known when the program is written

Variables: Name given to a value. The value may change when the program is executed

Reserve words: if, then, else, return, float, real Procedures: Functions or subroutines, which

may or may not have arguments and may or may not return values

Page 3: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

ASSEMBLY LEVEL LANGUAGE CONSTRUCTS Computer’s memory: A sequence of bytes,

words, blocks. Each computer have a fixed amount of working memory

CPU’s registers: Each computer have a set of working as well as special registers

Instructions: Each assembly language must have a set of instructions based on the microprocessor used in the computer, along with additional instructions available in the assembler

Location Counter: Its task is to keep track of the current location of the instruction being processed relative to the beginning of the next group of instructions

Page 4: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

HOW TO RUN ASSEMBLY PROGRAMS ?

Step 1 : Enter the assembly language program through a text editor or an inbuilt editor of the assembler. The inbuilt editor is known as work bench program

Step 2 : The assembly program file or the source file is a ASCII file having the extension .ASM

Step 3 : The assembler accepts this source file and converts it into an object file .OBJ in machine language. The linker converts such object file(s) into some executable form, such as .EXE, .BIN, etc through different passes. Sometimes the extension of inclusion file is .INC

Page 5: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

SOME COMMERCIAL ASSEMBLERS Borland turbo assembler (TASM) : This is a MS-

DOS based assembler. Suppose MYTASM.ASM is a source program in TASM syntax, then the command C:\TASM>TASM MYTASM assemblers the source program and produces MYTASM.OBJ, provided the assembly program is correct.

Microsoft assembler (MASM) : The name has traditionally been referred to the Microsoft Macro assembler. The software for MASM contains an executable program file ML.EXE. Here, ML assembles and links one or more assembly language source program files, producing a .OBJ file and an executable .EXE file

Page 6: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

SOME COMMERCIAL ASSEMBLERS

MicroAsm : It is a windows-based Integrated Development Environment(IDE) for the assembly programmer. This includes a source editor and an Intel 8086 assembler with easy syntax

Netwide Assembler(NASM) : It is a 80x86 processor-based assembler. It supports a wide variety of object file formats such as a.out, ELF, COFF, Win32, etc. Simple binary files are also generated by this assembler. This assembler works under different operating environments such as MS-DOS, WINDOWS and LINUX

Page 7: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

ASSEMBLER TYPES

One pass assemblers Two pass assemblers One and half pass assemblers Multi-pass assemblers

Page 8: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

ASSEMBLY LANGUAGE FORMATS Different assembly language instruction formats

differ on details like pseudo operations used, length of identifiers, formats of a constant, etc

However, most assembly language instructions has 2 mandatory and 2 optional fields.

Label : a symbol of the assembly program. Can store maximum of 6-8 characters

Opcode : contains mnemonic code for machine or pseudo operations

Operand : specifies 0 or more operands, separated by commas

Comments : high-level description of the program

[Label] Opcode Operand [Comments]

Page 9: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

HOW ASSEMBLY PROGRAMS ARE EXECUTED?

Consider the assembly program shown below [to be executed in Intel 8086 machine]:

Stm. No. Label Opcode Operand

1 0RG 100h

2 NUM1 DW 20

3 NUM2 DW 13

4 SUM DW ?

5 MOV AX, NUM1

6 ADD AX, NUM2

7 MOV SUM,AX

8 END

Red color indicates machine operations, Blue color indicates pseudo operations

For your understanding

Page 10: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

MOTAB & POTAB

Mnemonic Opcode Length(byte)

MOV AX,memo B8 3

ADD AX,memo 02 3

MOV memo,AX A2 3

Pseudo operation

Task

ORG Start the program and initialize LC with the value of operand, else initialize it to 0

DW Define the constant and word storage requirement. Update LC value

END End indicates the end of program. Also, enables allocation of addresses for the unallocated literals in the literal table. Control send to the next pass

Page 11: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

PASS 1 OF THE ASSEMBLY PROCESS

Stm. No. 1 : LC ← 100h Stm. No. 2 : NUM1 in symbol table with value 100h LC ← 100h + 2h = 102h Stm. No. 3 : NUM2 in symbol table with value 102h LC ← 102h + 2h = 104h Stm. No. 4 : SUM in symbol table with value 104h LC ← 104h + 2h = 106h Stm. No. 5: LC ← LC + 3h =109h Stm. No. 6 : LC ← LC + 3h =10Ch Stm. No. 7 : LC ← LC + 3h =10Fh Stm. No. 8: Opcode is END, indicating end of

program

Page 12: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

PASS 2 OF THE ASSEMBLY PROCESS

Stm. No. 1 : Allocate the LC ← 100h Stm. No. 2-4 : Already processed in pass 1 and

symbols stored in symbol table

Stm. No. 5: Machine code= B8 + Address of NUM1 = B80100

Stm. No. 6 : Machine code= 02 + Address of NUM2 = 020102

Stm. No. 7 : Machine code= A2 + Address of SUM = A20104

Stm. No. 8: Opcode is END, indicating termination of program

Page 13: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

SUMMARY : THE TWO PASSES

Pass I is to construct a symbol table and a literal table using the MOTab and POTab

Pass I keeps a copy of the user’s program for Pass II

An intermediate mnemonic program is output of Pass I

The task of Pass II is to generate the object code from the copy of the user’s program in Pass I

Pass II provides the linker & loader with the necessary information for linking object code with link libraries and ultimately loading the executable code in memory for execution

Page 14: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

PRACTICE PROBLEM

Generate symbol table (Pass 1) and machine code(Pass 2) for the assembly program below running on a SIC machine [Note: SIC is a 24-bit machine]Stm.

No.Label Opcode Operan

d

1 SIC START 02

2 LDA NUM1

3 ADD NUM2

4 STA SUM

5 NUM1 WORD 20

6 NUM2 WORD 13

7 SUM WORD 0

8 ENDRed color indicates machine operations, Blue color indicates pseudo

operations

Page 15: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

MOTAB & POTAB FOR PRACTICE PROBLEM

Mnemonic Opcode Length(byte)

LDA m 00 3

ADD m 18 3

STA m 0C 3

Pseudo operation

Task

START Start the program and initialize LC with the value of operand, else initialize it to 0

WORD Define the constant and storage requirement. Update LC value

END End indicates the end of program. Also, enables allocation of addresses for the unallocated literals in the literal table. Control send to the next pass

Page 16: S YSTEMS P ROGRAMMING CHAPTER 2 PROGRAMMING IN ASSEMBLY LANGUAGE Er. Bharadwaj Choudhury.

THANK YOU