Chapter 5 - Basic Concepts of Assembly Language PDF

Summary

This document provides an overview of assembly language programming concepts. It explains the different levels of software hierarchy, the role of assembly language, and its applications. The document also details machine instructions and highlights the relationship between assembly language and high-level languages.

Full Transcript

TOPIC 5 Basic Concept of Assembly Language Programming Software Hierarchy Levels Level Description Application Program Software designed for a particular class of applications High-Level Lang...

TOPIC 5 Basic Concept of Assembly Language Programming Software Hierarchy Levels Level Description Application Program Software designed for a particular class of applications High-Level Language (HLL) Programs are compiled into either assembly language or machine language. E.g. C++, Pascal, Java, Visual Basic, etc. Operating Systems Contains procedures than can be called from programs written in either high-level language or assembly language. This system may also contain an application programming interface (API). Assembly Language (ASM) Uses instruction mnemonics that have a one-to- one correspondence with machine language. E.g. C, C++, ML Machine Language (ML) Numeric instructions and operands that can be stored in memory and directly executed by the computer processor. IKB20803 Computer Organization What is Assembly Language (AL)? To learn how a computer and its software really work, you need to view them at machine level. Assembly language is a specific set of instructions for a particular computer system. ALteaches you about the way the computer’s hardware and OS work together and how application programs communicate with the OS. It is a programming language with a one-to-one correspondence between its statement and a computer’s main machine language. IKB20803 Computer Organization 3 What is Assembly Language (AL)? Each computer or family of computers uses a different set of machine instructions and a different assembly language (the computer’s design influences the instructions it can execute). Anassembler – a program that converts or translates source- code programs into machine language, which may in turn be executed by the computer (runs under the disk operating systems MS-DOS or PC-DOS). IKB20803 Computer Organization 4 What is Assembly Language (AL)? A low-level processor- specific programming language design to match the processor’s machine instruction set each assembly language instruction matches exactly one machine language instruction Intel’s 80x86 (and Pentiums) IKB20803 Computer Organization A Hierarchy of Languages 6 IKB20803 Computer Organization What is High-Level Language? A high-level language is a programming language such as Java, Visual Basic, C, FORTRAN, or Pascal that enables a programmer to write programs that are more or less independent of a particular type of computer. Such languages are considered high- level because they are closer to human languages and further from machine languages IKB20803 Computer Organization Machine Instructions A machine instruction is a binary code that has a special meaning for a computer’s CPU – it tells the computer to perform a task. Eachmachine instruction is precisely defined when the CPU is constructed, and it is specified to that type of CPU. For example: 00000100 Add a number to the AL register. 10100011 Move the AX register to another register IKB20803 Computer Organization 8 Machine Instructions Registersare high-speed storage locations inside the CPU which are used by nearly every instructions Theyare identified by 2-letter names, such as AH, AL, AX and so on. We refer to machine instructions using hexadecimal numbers because they take up less writing space. IKB20803 Computer Organization 9 Machine Language An assembler is a program that converts ASM code into machine language code: mov al,5 (Assembly Language) 1011000000000101 (Machine Language) most significant byte is the opcode for “move into register AL” the least significant byte is for the operand “5” Directly programming in machine language offers no advantage (over Assembly) IKB20803 Computer Organization Assembly Language Applications Application programs are rarely written completely in assembly language only time-critical parts are written in ASM Ex: an interface subroutine (called from HLL programs) is written in ASM for direct hardware access Ex2: device drivers (called from the OS) ASM often used for embedded systems (programs stored in PROM chips) computer cartridge games, microcontrollers (automobiles, industrial plants...), telecommunication equipment… IKB20803 Computer Organization Very fast and compact but processor-specific Assembly Language Instructions Mnemonic – a short alphabetic code that literally “assists the memory” in remembering CPU instruction It may be an instruction or a directive E.g. an instruction MOV (move) E.g. a directive DB (definite byte, used to create memory variables) Aninstruction may contain zero, one or two operands. IKB20803 Computer Organization 12 Intel 8086 Architecture There are 14 internal registers in Intel 8086 All of them are represented in 16-bit mode or equivalent to 4 digits in hexadecimal They are grouped into several categories For general-purpose registers, each of these is a combination of two 8-bit registers which are separately accessible as AL, BL, CL, DL (the "low'' bytes) and AH, BH, CH, and DH (the "high'' bytes) For example, if AX contains the 16-bit number 1234h, then AL contains 34h and AH contains 12h IKB20803 Computer Organization 13 Registers Instruction PC / Pointer IP General-purpose Special-purpose segment registers Status register registers registers AX = AH + AL, SP, BP, SI, DI CS, DS, ES, SS FLAGS BX = BH + BL , CX = CH + CL, DX = DH + DL Figure 1: 14 Registers in Intel 8086 IKB20803 Computer Organization 14 General Purpose Registers There are 4 general-purpose registers, each of them is designed to play a particular role in common use: AX – Accumulator Register Used for operations involving i/o and most arithmetic such as MUL and DIV, require that one of the operands be in the accumulator As a place to store data BX – Base Register The only general-purpose register which may be used for indirect addressing, MOV [BX], AX As a place to store data CX – Count Register It may contain a value to control the number of times a loop is repeated or a value to shift/rotate bits left or right As a place to store data DX – Data Register Used together with AX for the word-size MUL and DIV operations IKB20803 Computer Organization 15 As a place to store data Special Purpose Registers There are 4 special-purpose registers: Stack Pointer Register (SP) Holds add of top of stack Base Pointer Register (BP) Holds add of ref point in the stack Source Index Register (SI) Holds memory add of source Destination Index Register (DI) Holds memory add of destination IKB20803 Computer Organization 16 Segment registers There 4 segment register: Code Segment Register (CS) Holds selector for code segment Data Segment Register (DS) Holds selector for data segment Extra Segment Register (ES) Holds selector for extra segment Stack Segment Register (SS) Holds selector for stack segment IKB20803 Computer Organization 17 Status Register Thestatus register, FLAGS, is a collection of 1-bit values, which reflect the current state of the processor and the results of recent operations 9 of the 16 bits are used in the 8086. In this lesson you will only learn 6 flags: Carry flag Parity flag Sign flag Zero flag Overflow flag Auxiliary Flag IKB20803 Computer Organization 18 Instruction Pointer Theinstruction pointer (sometimes called Program Counter - PC), IP, gives the address of the next instruction to be executed, relative to the code segment. IKB20803 Computer Organization 19 Debug Commands Command Purpose r Display the contents of all registers Alter the contents of a register u List the instructions contained in the given address and following in assembly language form D Display the contents of memory in hexadecimal format and as ASCII character starting with the address given e Enter values into memory, beginning at a specific location t Trace the execution of an instruction a Choose an address to write a program q Quit the debug session IKB20803 Computer Organization Assembly and Machine Language Machine language Native to a processor: executed directly by hardware Instructions consist of binary code: 1s and 0s Assembly language A programming language that uses symbolic names to represent operations, registers and memory locations. Slightly higher-level language Readability of instructions is better than machine language One-to-one correspondence with machine language instructions Assemblers translate assembly to machine code Compilers translate high-level programs to machine code Either directly, or Indirectly via an assembler IKB20803 Computer Organization 21 Compiler and Assembler IKB20803 Computer Organization 22 Assembly and Machine Language Code IKB20803 Computer Organization 23 Instructions and Machine Language Each command of a program is called an instruction (it instructs the computer what to do). Computers only deal with binary data, hence the instructions must be in binary format (0s and 1s). The set of all instructions (in binary form) makes up the computer's machine language. This is also referred to as the instruction set. IKB20803 Computer Organization 24 Instruction Fields Machine language instructions usually are made up of several fields. Each field specifies different information for the computer. The major two fields are: Opcode field which stands for operation code and it specifies the particular operation that is to be performed. Each operation has its unique opcode. Operands fields which specify where to get the source and destination operands for the operation specified by the opcode. The source/destination of operands can be a constant, the memory or one of the general-purpose registers. IKB20803 Computer Organization 25 Assembly vs. Machine Code 26 IKB20803 Computer Organization Instructions Op code Operation code Arbitrary mnemonic Operand Object to be manipulated Data or Address of data Address Content Op code Operand IKB20803 Computer Organization 27 Instruction Set Arithmetic 1xx ADD 2xx SUB Data Movement 3xx STORE 5xx LOAD Input/Output 901 INPUT 902 Output Machine Control 000 STOP (coffee break) COB IKB20803 Computer Organization Input/Output Move data instructions (Input/Output) Content Op Code Operand (address) IN (input) 9 01 OUT (output) 9 02 IKB20803 Computer Organization 29 Mapping Between Assembly Language and HLL Translating HLL programs to machine language programs is not a one-to-one mapping A HLL instruction (usually called a statement) will be translated to one or more machine language instructions 30 IKB20803 Computer Organization Assembly vs. High-Level Languages  Some representative types of applications: IKB20803 Computer Organization 31 Assembly Language Programming Tools Software tools are used for editing, assembling, linking, and debugging assembly language programming. You will need an assembler, a linker, a debugger, and an editor. Assembly-, Machine-, and High-Level Languages IKB20803 Computer Organization 32 Assembler Software tools are needed for editing, assembling, linking, and debugging assembly language programs Anassembler is a program that converts source-code programs written in assembly language into object files in machine language Popularassemblers have emerged over the years for the Intel family of processors. These include … TASM (Turbo Assembler from Borland) MASM (Microsoft Macro Assembler) NASM (Netwide Assembler for both Windows and Linux), and GNU assembler distributed by the free software foundation IKB20803 Computer Organization 33 Linker and Link Libraries You need a linker program to produce executable files It combines your program's object file created by the assembler with other object files and link libraries, and produces a single executable program LINK32.EXE is the linker program provided with the MASM distribution for linking 32-bit programs We will also use a link library for input and output Called Irvine32.lib developed by Kip Irvine Works in Win32 console mode under MS-Windows IKB20803 Computer Organization 34 Assemble and Link Process Source Object File Assembler File Source Object Executable File Assembler File Linker File Source Object Link File Assembler File Libraries A project may consist of multiple source files Assembler translates each source file separately into an object file Linker links all objectIKB20803 files together with link libraries Computer Organization 35 Debugger Allows you to trace the execution of a program Allows you to view code, memory, registers, etc. Example: 32-bit Windows debugger IKB20803 Computer Organization 36 Editor Allows you to create assembly language source files Someeditors provide syntax highlighting features and can be customized as a programming environment IKB20803 Computer Organization 37 Translating Languages English: Display the sum of A times B plus C. High Level Language … C++: count

Use Quizgecko on...
Browser
Browser