Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
COAL 1 [email protected] Computer Organization & Assembly language Pre Requisites: Digital Logic Design, Computer System Architecture Course Objective: The main objective of the course is to introduce students to the concepts of Assembly Language programming and to give the students a knowledge and experience of low level programming with assembly language. Upon successful completion of this course, the student will be able to: Reference Book: Assembly Language for Intel Based Computers By Kip R Irvin 4th Edition IBM PC Assembly Language and Programming by Abel Course Coverage: The evolution of the computer organization, Application of assembly language How the operating system and programs work with application software Understanding of Assembly language Design and develop assembly program Implement programs using conditional structure Implement programs using loops Use of data transfer and arithmetic instructions Write subroutines and macros Duration: 15 weeks Assessment: Assignments: Quizzes : Project: Exam papers 2 4 1 2 COAL 2 [email protected] Week 1: lecture Contents Why learn Assembly language Application areas Assembly Vs Machine language Assembly VS HLL Introduction to Intel 16 bit registers Introduction “Computer is a magical box” is not just a phrase; rather it is a great reality. Isn’t this a magic that it can perform wonderful operation even with the help of a couple of bits 0, 1? Really wonderful!! It encodes everything (numbers, alphabets, sound, pictures, heat, light, pressure etc.) into 0, 1. It speaks and listens only in the form of 0,1….nothing else. That is why such a language is called machine language. It is quite amazing that the long sequences or chains of 0,1s are the easiest thing to learn and remember without any difficulty but the same sequences are a big challenge for a human being who claims to be the most intellectual creature on this earth. Can we say that computer has defeated man in the arena of language comprehension? Behold, that is not the issue with only human being, the same problem lies with computer too as all human languages are strange to computer. It can’t understand them directly too. Thus a translation approach has been adopted to facilitate both computer and human being. An assembly language is a first conscious effort to get rid of lengthy sequence of 0, 1. It replaced almost every binary sequence with certain unique symbol called mnemonics (generally one to one correspondence between binary codes and symbols). These symbols were easy to learn and remember thus made computer programming an easier job. However, for a program to run on computer these symbols must have to be reconverted back into binary codes (machine codes). The software that performs this type of translation from assembly language to machine language is known as assembler. Pros and corns of assembly programs A program written in assembly language need less translation time and translation process doesn’t add extra lines to the actual program, hence assembly program is highly efficient. However, one deficiency known to assembly programs is their portability issue. It is because of the strong correspondence (generally one-to-one) between the machine codes and their respective symbols (mnemonics). This 1-1 correspondence means that each assembly language is specific to particular computer architecture. Assembly language for designed for an architecture doesn’t fit on other architecture that is why assembly programs are not portable across different architectures. *** Assembly language symbolizes the computer’s numeric world for the ease of remembering*** Why should we learn assembly language? When there are higher level languages one better than the other; C, C++, Java, to name just a few, with a neat programming environment and a simple way to write programs, then why do we need such a freehand with the computer that may be dangerous at times? The answer to this lies in a very simple example. Consider an arch just like the ones in mosques. It cannot be made of big stones alone as that would make the arch wildly jagged, not like the fine arch we are used to see. The fine grains of cement are used to smooth it to the desired level of perfection. This operation of smoothing is optimization. The core structure is built in a higher level language with the big COAL 3 [email protected] blocks it provides and the corners that need optimization are smoothed with the fine grain of assembly language which allows extreme control. Application Areas There are 2 major application areas of assembly language. Real time systems Code optimization Though, HLL have a neat and easier environment for programming yet there are some situations where they fail to produce efficiency. For example, Real time systems have time bound responses, with an upper limit of time on certain operations. For such precise timing requirement, we must keep the instructions in our total control. Code optimization is another application area of assembly language. Any reasonable sized application or a serious development effort has nooks and corners where assembly language is needed. And at these corners if there is no assembly language, there can be no optimization and when there is no optimization, there is no beauty. Sometimes a useful application becomes useless just because of the carelessness of not working on these jagged corners. Assembly VS machine language Machine language and assembly language both represent the same programming world but their style is different. Machine language uses “bit encoding” to represent cpu opcodes while assembly language uses “symbolic encoding” for this purpose. Machine language is directly understood by machine, but assembly language needs to be translated first into machine language by a translator program, assembler, before its actual execution. Machine language instruction are difficult to understand by human being while assembly language instructions are easier one. Assembly language vs HLL High level languages are abstract. Typically a single high level instruction is translated into several (sometimes dozens or in rare cases even hundreds) executable machine language instructions. Some early high level languages had a close correspondence between high level instructions and machine language instructions. Assembly programs have direct access to key machine features essential for implementing certain kinds of low level routines, such as an operating system kernel or microkernel, device drivers, and machine control. HLL programs lack such powers and are more formal by nature. HLL codes development and maintenance is easier than assembly language. Both needs to be translated into machine language before actual execution takes place. HLL programs are machine independent and portable while assembly programs are sensitive to machine architecture i.e machine dependent and are not portable across various machines. Address, Data, and Control Buses A computer system comprises of a processor, memory, and I/O devices. I/O is used for interfacing with the external world, while memory is the processor’s internal world. Processor is the core in this picture and is responsible for performing operations. The scenario that the processor executes operations and the memory contains data elements requires a mechanism for the processor to read that data from the memory. There must be a mechanism to inform memory that we want to do the read operation COAL 4 [email protected] There must be a mechanism to inform memory that we want to read precisely which element There must be a mechanism to transfer that data element from memory to processor The group of bits that the processor uses to inform the memory about which element to read or write is collectively known as the address bus. Another important bus called the data bus is used to move the data from the memory to the processor in a read operation and from the processor to the memory in a write operation. The third group consists of miscellaneous independent lines used for control purposes. For example, one line of the bus is used to inform the memory about whether to do the read operation or the write operation. These lines are collectively known as the control bus. The address bus is unidirectional and address always travels from processor to memory. This is because memory is a dumb device and cannot predict which element the processor at a particular instant of time needs. Data moves from both, processor to memory and memory to processor, so the data bus is bidirectional. Control bus is special and relatively complex, because different lines comprising it behave differently. REGISTERS Registers are temporary memory locations inside the cpu which hold operands or intermediary results. The number of bits in a register may vary from 4, 8, 16 to 64 bits depending on the architecture. Currently, we are going to study Intel 8088 architecture which consists of 14 registers in total as shown in figure. It is noteworthy that all registers are 16 bit. 8 Bits 8 Bits AH AL AX Accumulator BH BL BX Base CH CL CX Count DH DL DX Data SP Stack Pointer BP Base Pointer Pointer and SI Source Index Index DI Destination Index Registers IP Instruction Pointer CS Code Segment DS Data Segment Segment SS Stack Segment Registers ES Extra Segment General Purpose Register Flags 16 Bits General Purpose Registers: . These are used in any manner that the programmer wishes. These registers include AX, BX, CX and DX. Their lower and higher portions can be used separately by programmer. AX: Stands for extended accumulator register which holds temporary results during or after an arithmetic or logical operation. BX: stands for extended base register which holds the base address of data located in memory. CX: Stands for extended Count register and hold the count for certain instruction like counter in loop instruction. COAL 5 [email protected] DX: Stands for data register. It holds the most significant part of 16 bit multiplication and most significant part of dividend before division. Pointer and Index Registers. Although the pointer and index registers are also general purpose in nature, they are more often used to index or point to the memory location holding the operand data for many instructions. SP (Stack Pointer). This is used to address data in a LIFO (last-in first-out) stack memory. This occurs most often when the PUSH and POP instructions are executed. BP (Base Pointer). This is often used to address an array of data in memory. SI (Source Index). This is used to address source data indirectly for use with string instructions DI (Destination Index). This is normally used to address destination data indirectly for use with the string instructions. Status Register or Processor Status Word. This contains 16 bits, but 7 of them are not used. Each bit in the PSW is a flag. These flags are divided into the conditional flags (they reflect the result of the previous operation involving the ALU) and the control flags (they control the execution of special functions). The conditional flags are: 1. Sign Flag (SF) - b7 This is equal to the MSB of the result of the previous operation. 0 if positive, 1 if negative. 2. Zero Flag (ZF) - b6 This is set to 1 if the result of the previous operation is zero and 0 if the result is nonzero. 3. Parity Flag (PF) - b2 COAL 6 [email protected] This is set to 1 if the low-order 8 bits of the result of the previous operation contain an even number of 1s. Otherwise, it is reset to 0. 4. Carry Flag (CF) - b0 An addition causes this flag to be set to 1 if there is a carry out of the MSB, and a subtraction causes it to be set to 1 if a borrow is needed. 5. Overflow Flag (OF) - b11 This is set to 1 if an overflow occurs, i.e., a result is out of range. More specifically, for addition this flag is set to 1 when there is a carry into the MSB and no carry out of the MSB or vice-versa. For subtraction, it is set to 1, when the MSB needs a borrow and there is no borrow from the MSB, or vice-versa. 6. Auxiliary Carry Flag (AF) - b4 This flag is used exclusively for BCD arithmetic. It is set to 1 if there is a carry out of bit 3 (b3) during an addition or a borrow by bit 3 during a subtraction. Examples: 0010 0011 0100 0101 + 0011 0010 0001 1001 0101 0101 0101 1110 SF = 0 ZF = 0 PF = 0 CF = 0 AF = 0 OF = 0 1101 0111 1011 1001 + 1101 0111 0111 1010 1010 1111 0011 0011 COAL 7 [email protected] SF = 1 ZF = 0 PF = 1 CF = 1 AF = 1 OF =1 1110 1011 1110 0110 + 1011 1111 0110 1100 1010 1011 0101 0010 SF = 1 ZF = 0 PF = 0 CF = 1 AF = 1 OF =1 0101 0110 0111 1110 - 0100 1110 1110 1010 0000 0111 1001 0100 SF = 0 ZF = 0 PF = 0 CF = 0 AF = 0 OF = 0 The control flags are: 1. Direction Flag (DF) - b10 This flag is used by string manipulation instructions. If clear, the string is processed from its beginning with the first element having the lowest address. Otherwise, the string is processed from the high address towards the low address. 2. Interrupt Enable Flag (IF) - b9 If set, a certain type of interrupt (a maskable interrupt) can be recognized by the CPU; otherwise, these interrupts are ignored. 3. Trap Flag (TF) - b8 If set, the 8086/8088 will enter into a single-step mode. In this mode, the CPU executes one instruction at a time.