Unformatted text preview:

Figure 1: Assembler’s processAssembly Language• Assembler translates the assembly language source into binary instructions in an object file.• Programs usually contain multiple assembly/HLL source files, called modules, each com-piled and assembled independently. Also, precompiled routines in library. So, an objectmodule (.o files for e.g.) contains external references to routines in other modules andlibraries and therefore can’t be executed since these are unresolved references. Linkercombines .o files and libraries to produce an executable.• Fig. 2 show machine code for a program, Fig. 3 in raw AL, Fig. 4 in AL with labels foraddresses. Names beginning with a dot are assembler directives.– .text says that following lines are instructions– .align n says align lines on a 2nbytes boundary. .align 2 means align on a wordlength (each line is a word long).– .data indicates that data lines follow– .asciiz stores a null terminated string in memory, .globl declares a procedure avail-able to other object files• AL serves dual purposes in practice:– output of HLL. the result of compilation, ∴ the target language to the source languageof HLL– language of programming. if∗ speed is critical. for e.g., in real-time systems. Also embedded systems (the com-puter is part of a device). Compiler may translate HLL code in indeterminate waysso it is difficult to judge the time cost. AL code however is the (almost) actual codethat runs on the CPU.∗ exploit hardware features often hidden to HLL, for e.g., low-level memory address-ing.1Figure 2: Machine code for adding integers between 0 and 100Figure 3: Assembly language2Figure 4: Assembly language with labels3• Another approach: code in both HLL and AL. Use program profiling (automatic or man-ual) to find time-critical parts of program (most time spent). Can improve so much by usingbetter data structures or algorithms. Best: do these parts in AL.• Compilers are usually better at producing uniform high quality code. Programmers canhowever consider writing (AL) code in many different ways thus coming up with a besttechnique.– Compilers are becoming smarter though.• Also AL is the only lang. available on some legacy or embedded systems• Problems with using AL to code a program:– Tightly bound to the architecture for which it was generated. May become obsolete.HLL code can be compiled for any architecture that has a compiler.– Longer. ∴, less productive; difficult to understand (try finding out what the loop doesin sample code) since no structure, conditionals or loops must be built from scratch(using basically gotos); more bugs.– difficult to verify the correctness of program. Ada was developed as a HLL for embeddeddevices (similarly J2ME).Assemblers• assembly code → object file w/ binary instructions and data. Two steps:– find labels with memory locations (keeps this bookkeeping info (relocation information)so local references can be resolved)– translate each AL instruction to (binary) numeric instruction• object files do not have external references resolved (assembler only resolves in-file references)so can’t be run directly. (although it IS in binary format)• Linker combines multiple objects by resolving external references in between them. Assem-bler helps by keeping a symbol table, a list of undefined external references.• Assembler’s 1st pass: build up symbol table.– break instruction into lexemes: ble $t0, 100, loop contains 6.– If line begins with label (loop labels, procedure id labels), records label and the memoryword address for instructn in symbol table.– records instruction’s and data’s size• 2nd pass: examines each line again; actually builds m/c code using symbol table– Instructions are translated to binary (opcode + operand), including those that use locallabels defined in symbol table.– If label is for external reference, leaves it be, since symbol table doesn’t have it.4• Assembler assumes memory starts from 0.• Some instructions (jal, lwl/h/b, sw/h/b) require absolute addresses of operand data orinstruction, and assembler doesn’t know actual addresses upon loading. A file’s data andinstructions are stored contigously in memory but starting address is not known beforehandto assembler.– Assembler ∴ builds relocation information. List of instructions in .o file that needabs. addresses. These are relocated by the linker.Facilities• Assemblers provide some extra facilitites. E.g.: directives: which are instructions meant forassembler (not part of AL)• e.g. data layout directives:.asciiz \The sum from 0 .. 100 is %d\n"• Macros. pattern matching and code replacement– Like methods/functions. However, only replacement, not actual method call.– For e.g., a print program that prints, in this case, the value in register $7 ($t9).dataint_str:.asciiz "%d".textla $a0, int_str # Load string address# into first argmov $a1, $7 # Load value into# second argjal printf # Call the printf routineCan be implemented using a macro.dataint_str:.asciiz "%d".text.macro print_int($arg)la $a0, int_str # Load string address into# first argmov $a1, $arg # Load macro’s parameter# ($arg) into second argjal printf # Call the printf


View Full Document

MU CPTR 308 - Assembly Language

Download Assembly Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Assembly Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Assembly Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?