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
1/4/2013 Introduction to Programming in Assembler Language Lecture 4 What Does a Microprocessor Know? A microprocessor “understands” only binary information; numbers and characters are just patterns of 0’s 0 s and 1 1’s; s; program instructions are just patterns of 0’s and 1’s SUBQ.L #1,D2 => 0x5382 = 0b0101001110000010 Lecture 4 2 1 1/4/2013 Machine Language • • Not all possible binary patterns may correspond to legal CPU instructions For the Coldfire architecture, instructions occupy anywhere from 1 to 3 subsequent words in memory, - where each word has 16 bits Lecture 4 3 Machine Language (Cont’d) For a MOVE instruction: move.l #0x20000, d1 movement Byte: 01 Reg #: 001 Data reg: Word: 11 000 Long:10 Immediate data: 111 100 .org 0x1000 move.l #0x20000,d1 .end => 0x1000 22 3c 0x1001 0x1002 00 02 0x1003 0x1004 00 00 0x1005 Lecture 4 4 2 1/4/2013 Machine Language (Cont’d) bits 15-14: 00, which means a data movement bits 13-12: size. Since the size is longword, here bits 13-12 are 10 bits 11-6 bi 11 6 are for f d destination, i i where h bits bi 8-6 8 6 are to specify if the h "mode" " d " off the destination (i.e., is the destination a data register, an address register, or memory location?). Since here the destination is a data register D1, the three destination mode bits are 000, which are corresponding to data register. Bits 11-9 are destination register number, which are used to specify "which" data register is the destination. Since here we have data register #1, bits 11-9 are 001, which is the binary code for number 1. bits 5-0 are for source source, where bits 5-3 are mode mode, and bits 2-0 are register number. If the operand is an immediate number, the mode bits are always 111, and the register number bits are always 100. Therefore, the operation word for the example data movement instruction is: 0010 001000 111100 in binary, which is 0x22 3C. Lecture 4 5 Machine Language (Cont’d) In addition, the six bits for the source (i.e., bits 5-0) only tell that the source is an immediate number. But they do not tell "what" value it is. Therefore, we need extension words to tell what value the immediate number b is. i Since Si the h operation i value l is i a long l word d (as ( specified ifi d by b .L), L) we write the source value in a longword format, which is 0x 0002 0000. Then, we need two extension words to specify what the longword-sized immediate number is. Since the first word is the most significant word, the first extension word is 0x 0002, while the second extension words is 0x 0000. In a summary, we need three words for the example data movement instruction which are 0x 223C 0002 0000. instruction, 0000 Lecture 4 6 3 1/4/2013 Assembly Language • Assembly Language is a symbolic, more “human friendly” notation for expressing programs made up of machine language instructions; move.b, move.w, clr.l • Instruction mnemonics, decimal numbers, alphanumeric labels can all be used start add #1, D0 move.l A, D0 Lecture 4 7 Assembly vs. Machine Languages Address Contents 8000 8002 8004 8006 8008 800A 800C 800E 8010 7200 227C 0002 0000 7404 D299 5382 66FA 4E4F INIT LOOP MOVEQ.L #0,D1 MOVEA.L #0x20000,A1 ; second word ; third word MOVEQ.L #4,D2 ADD.L (A1)+,D1 SUBQ.L #1,D2 BNE LOOP TRAP #15 INIT is the name for address location 0x8000 LOOP is the name for address 0x800A Lecture 4 8 4 1/4/2013 Assembly Language A mnemonic is an aid to memory • “clear a byte of data register D5” in binary is 0b 0100 0010 0000 0101 or in hexadecimal 0x 4205 • The mnemonic representation is: • 0b: binary 0x: hex CLR.B D5 much more easy to understand - Addresses and constants can be written in a symbolic form A valid symbolic name is created by the programme, e.g. Temp1, A, B, Var_01 • • Lecture 4 9 Assembly Language Syntax • create an assembly language source file using any text editor vi, kwrite, emacs, winedit, notepad, etc. • An assembly language instruction is one of two types: • an executable instruction: a valid processor instruction, e.g. MOVE.B D0, D7 ADD.L D5, A7 • an assembler directive: it is not a processor instruction, is not translated into machine code (binary), 1) link symbolic names to actual values, e.g., .equ TEN, 10 2) allocate storage for data in memory, e.g., ds.w 4 3) set up a space for pre-defined constants, e.g., .byte 23 Lecture 4 10 5 1/4/2013 Assembly Language Syntax • An assembly language instruction has up to four fields: label opcode operands comment • label is used to reference this instruction; it also becomes the name of the address where the instruction is stored; • opcode is the machine operation code • E.g. ADD, add, MOVE, CLR • operands indicate the locations of source data and result of the operation • comment is a comment to assist the reader in understanding what the instruction is about, indicated by /*…*/ /* This is the beginning of the main algorithm */ Lecture 4 11 Assembly Language Syntax EXAMPLE .org 0x001000 /*program to start at 0x1000 */ /* this is an example */ start move.l d0, d1 /*this instruction copies the*/ /*contents of d0 into d1 */ /*the RTL notation is [d1] ←[d0] */ Lecture 4 12 6 1/4/2013 Basic Concepts • Assembling • • • Is the process of translating an assembly language program into a machine language program; “hand assembling” is when a human assembles the program without using any program aids. Assembler • Is a program that automates the process of translating an assembly language program into the corresponding machine language program Lecture 4 13 Basic Concepts • Disassembling • • Lecture 4 Is the reverse p process to assembling g in which an assembly language program is reconstructed from a machine language program; disassembling Can be useful for debugging and reverse engineering 14 7 1/4/2013 Basic Concepts • • • If the assembler runs on the same machine that runs the machine code it is called a resident assembler If the assembler runs on another machine it is called a cross – assembler In this situation the code must be transferred from the host computer (where the assembler runs) to the target machine (where the object code runs). This process is called down loading Lecture 4 15 Basic Concepts • • Lecture 4 The process of moving object code into memory and making it ready to run is called loading Some assemblers permit programs to be broken into several files which can be assembled independently; a program which combines several object files into one executable file is called a linker 16 8