Assembly language programming

Slides:



Advertisements
Similar presentations
Register In computer architecture, a processor register is a small amount of storage available on the CPU whose contents can be accessed more quickly than.
Advertisements

Registers of the 8086/ /2002 JNM.
There are two types of addressing schemes:
Assembly Programming Notes for Practical2 Munaf Sheikh
Introduction to 8086 Microprocessor
Azir ALIU 1 What is an assembly language?. Azir ALIU 2 Inside the CPU.
8086 Assembly Language Programming I
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
An Introduction to 8086 Microprocessor.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
Types of Registers (8086 Microprocessor Based)
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Click to add Title Comunicación y Gerencia Click To add Subtitle Click to add Text Fundamentals of Assembly Language.
Review of Assembly language. Recalling main concepts.
MODULE 5 INTEL TODAY WE ARE GOING TO DISCUSS ABOUT, FEATURES OF 8086 LOGICAL PIN DIAGRAM INTERNAL ARCHITECTURE REGISTERS AND FLAGS OPERATING MODES.
Internal Programming Architecture or Model
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
I NTEL 8086 M icroprocessor بسم الله الرحمن الرحيم 1.
Chapter 12 Processor Structure and Function. Central Processing Unit CPU architecture, Register organization, Instruction formats and addressing modes(Intel.
Instruction set Architecture
Format of Assembly language
Part of the Assembler Language Programmers Toolbox
Assembly Language programming
Introduction to 8086 Microprocessor
8086 Microprocessor.
Computer Organization & Assembly Language Chapter 3
COURSE OUTCOMES OF MICROPROCESSOR AND PROGRAMMING
Additional Assembly Programming Concepts
ADDRESSING MODES.
Microprocessor and Assembly Language
Microprocessor Systems Design I
Microprocessor and Assembly Language
Microprocessor and Assembly Language
ADDRESSING MODES.
University of Gujrat Department of Computer Science
Intel 8088 (8086) Microprocessor Structure
(The Stack and Procedures)
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Programming 8086 – Part IV Stacks, Macros
Intel 8088 (8086) Microprocessor Structure
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
CS 301 Fall 2002 Computer Organization
(The Stack and Procedures)
Shift & Rotate Instructions)
The Microprocessor & Its Architecture
Symbolic Instruction and Addressing
Morgan Kaufmann Publishers Computer Organization and Assembly Language
CNET 315 Microprocessor & Assembly Language
Computer Architecture CST 250
Unit-I 80386DX Architecture
Assembler Directives end label end of program, label is entry point
Chapter 6 –Symbolic Instruction and Addressing
Process.
(The Stack and Procedures)
Intel 8086.
Chapter 8: Instruction Set 8086 CPU Architecture
Introduction to 8086 Assembly Language
Part I Data Representation and 8086 Microprocessors
Chapter 10 Instruction Sets: Characteristics and Functions
Presentation transcript:

Assembly language programming Learning assembly language programming will help understanding the operations of the microprocessor To learn: Need to know the functions of various registers Need to know how external memory is organized and how it is addressed to obtain instructions and data (different addressing modes) Need to know what operations (or the instruction set) are supported by the CPU. For example, powerful CPUs support floating-point operations but simple CPUs only support integer operations UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

How to learn programming C –Concept L – Logic thinking P – Practice Concept – we must learn the basic syntax, such as how a program statement is written Logic thinking – programming is problem solving so we must think logically in order to derive a solution Practice – write programs UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Assembly Program The native language is machine language (using 0,1 to represent the operation) A single machine instruction can take up one or more bytes of code Assembly language is used to write the program using alphanumeric symbols (or mnemonic), eg ADD, MOV, PUSH etc. The program will then be assembled (similar to compiled) and linked into an executable program. The executable program could be .com, .exe, or .bin files UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

Flow of program development .asm Object file .obj Executable file .exe link Assemble UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Example Machine code for mov AL, 00H B4 00 (2 bytes) After assembled, the value B400 will be stored in the memory When the program is executed, then the value B400 is read from memory, decoded and carry out the task UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Assembly Program Each instruction is represented by one assembly language statement The statement must specify which operation (opcode) is to be performed and the operands Eg ADD AX, BX ADD is the operation AX is called the destination operand BX is called the source operand The result is AX = AX + BX When writing assembly language program, you need to think in the instruction level UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Example In c++, you can do A = (B+C)*100 In assembly language, only one instruction per statement A = B ; only one instruction - MOVE A = A+C ; only one instruction - ADD A = A*100 ; only one instruction - Multiply UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

Format of Assembly language General format for an assembly language statement Label Instruction Comment Start: Mov AX, BX ; copy BX into AX Start is a user defined name and you only put in a label in your statement when necessary!!!! The symbol : is used to indicate that it is a label UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 8086 Software Model UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Software model In 8086, memory is divided into segments Only 4 64K-byte segments are active and these are: code, stack, data, and extra When you write your assembly language program for an 8086, theoretically you should define the different segments!!! To access the active segments, it is via the segment register: CS (code), SS (stack), DS (data), ES (extra) So when writing assembly language program, you must make use of the proper segment register or index register when you want to access the memory UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Registers In assembly programming, you cannot operate on two memory locations in the same instruction So you usually need to store (move) value of one location into a register and then perform your operation After the operation, you then put the result back to the memory location Therefore, one form of operation that you will use very frequent is the store (move) operation!!! And using registers!!!!! UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Example In C++ A = B+C ; A, B, C are variables In assembly language A,B, C representing memory locations so you cannot do A = B+C MOV AL, B ; move value of B into AL register ADD, AL, C ; do the add AL = AL +C MOV A, AL ; put the result to A UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Data registers AX, BX, CX,and DX – these are the general purpose registers but each of the registers also has special function Example AX is called the accumulator – to store result in arithmetic operations Registers are 16-bit but can be used as 2 8-bit storage Each of the 4 data registers can be used as the source or destination of an operand during an arithmetic, logic, shift, or rotate operation. In some operations, the use of the accumulator is assumed, eg in I/O mapped input and output operations UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Data register In based addressing mode, base register BX is used as a pointer to an operand in the current data segment. CX is used as a counter in some instructions, eg. CL contains the count of the number of bits by which the contents of the operand must be rotated or shifted by multiple-bit rotate DX, data register, is used in all multiplication and division, it also contains an input/output port address for some types of input/output operations UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

Pointer and index registers Stack – is used as a temporary storage Data can be stored by the PUSH instruction and extracted by the POP instruction Stack is accessed via the SP (Stack Pointer) and BP (Base Pointer) The BP contains an offset address in the current stack segment. This offset address is employed when using the based addressing mode and is commonly used by instructions in a subroutine that reference parameters that were passed by using the stack UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

Pointer and Index Register Source index register (SI) and Destination index register (DI) are used to hold offset addresses for use in indexed addressing of operands in memory When indexed type of addressing is used, then SI refers to the current data segment and DI refers to the current extra segment The index registers can also be used as source or destination registers in arithmetic and logical operations. But must be used in 16-bit mode UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Data types Data can be in three forms: 8-bit, 16-bit, or 32-bit (double word) Integer could be signed or unsigned and in byte-wide or word-wide For signed integer (2’s complement format), the MSB is used as the sign-bit (0 for positive, 1 for negative) Signed 8-bit integer 127 to –128, For signed word 32767 to –32768 Latest microprocessors can also support 64-bit or even 128-bit data In 8086, only integer operations are supported!!! UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 A sample program .code ; indicate start of code segment .startup ; indicate start of program mov AX, 0 mov BX, 0000H mov CX, 0 mov SI, AX mov DI, AX mov BP, AX END ; end of file The flow of the program is usually top-down and instructions are executed one by one!!! UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Assembly programming In general, an assembly program must include the code segment!! Other segments, such as stack segment, data segment are not compulsory There are key words to indicate the beginning of a segment as well as the end of a segment. Just like using main(){} in C++ Programming Example DSEG segment ‘data’ ; define the start of a data segment DSEG ENDS ; defines the end of a data segment Segment is the keyword DSEG is the name of the segment Similarly key words are used to define the beginning of a program, as well as the end. UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

Assembly language programming Example CSEG segment ‘code’ START PROC FAR ; define the start of a program (procedure) RET ; return START ENDP ; define the end of a procedure CSEG ends End start ; end of everything Different assembler may have different syntax for the definition of the key words !!!!! Start is just a name it could be my_prog, ABC etc UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 More sample Stacksg segment para ‘stack’ …. ; define the stack segment Stacksg ends Datasg segment para …… ; declare data inside the data segment Datasg ends Codesg segment para ‘code’ Main proc far ; assume ss:stacksg, ds: datasg, cs:codesg mov ax, datasg mov ds, ax …. mov ax, 4c00H int 21H Main endp Codesg ends end main End of everything UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Definitions To declare a segment, the syntax is: segment_name SEGMENT alignment class Example Stacksg segment PARA (this statement is used in previous slide) PARA – define the alignment of the segment base address, the segment with a starting addressing that is evenly Divisible by 16. But the default value is also base address divisible by 16 so the key word PARA can be ignored! UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Definition ‘data’, ‘code’ – class entry. Is used to group related segments when linking. The linker automatically groups segments of the same class in memory PROC – define procedures (similar to a function) inside the code segment. Each procedure must be identified by an unique name. At the end of the procedure, you must include the ENDP UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Definitions FAR – is related to program execution. When you request execution of a program, the program loader uses this procedure as the entry point for the first instruction to execute. Assume – to associate, or to assign, the name of a segment with a segment register In some assembler, you need to move the base address of a segment directly into the segment register!!! END – ends the entire program and appears as the last statement. Usually the name of the first or only PROC designated as FAR is put after END UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

Syntax of a simple assembly language program If you are doing something simple then you do not need to define the segment Everything will be stored in the code segment UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 start: mov DL, 0H ; move 0H to DL mov CL, op1 ; move op1 to CL mov AL, data ; move data to AL step: cmp AL, op1 ; compare AL and op1 jc label1 ; if carry =1 jump to label1 sub AL, op1 ; AL = AL –op1 inc DL ; DL = DL+1 jmp step ; jump to step label1: mov AH, DL ; move DL to AH HLT ; Halt end of program data db 45 ; define a variable called data op1 db 6 ; define a variable called op1 UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Assembler for 8086 Emu8086 (http:// www.emu8086.com) – there is a trial version but it does not support all the features such as interrupt The emu8086 consists of a tutorial and the reference for a complete instruction set Keil – www.keil.com UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

Defining data in a program Data is usually stored in the data segment You can define constants, work areas (a chunk of memory ) Data can be defined in different length (8-bit, 16-bit) 8-bit then use DB 16-bit then use DW The definition for data: [name] Dn expression ; Dn is either DB or DW Name – a program that references a data item by means of a name. The name of an item is otherwise optional Dn – this is called the directives. It defines length of the data Expression – define the values (content) for the data UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01 Examples for data FLDA DB ? ; define an uninitialized item called FLDA 8-bit FLDB DB 25 ; initialize a data to 25 Define multiple data under the same name (like an array) FLDC DB 21, 22, 23, 34 ; the data are stored in adjacent bytes FLDC stores the first value FLDC + 1 stores the second value You can do mov AL, FLDC+3 UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01

Example for data definition DUP – duplicate DUP can be used to define multiple storages DB 10 DUP (?) ; defines 10 bytes not initialize DB 5 DUP (12) ; 5 data all initialized to 12 String : DB ‘this is a test’ EQU – this directive does not define a data item; instead, it defines a value that the assembler can use to substitute in other instructions (similar to defining a constant in C programming or using the #define ) factor EQU 12 mov CX, factor UNIT - 2 ASSEMBLY LANGUAGE PROGRAMMING 01