DOC PREVIEW
UCSC CMPE 012 - The Assembly Process

This preview shows page 1-2-3-18-19-37-38-39 out of 39 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

The Assembly ProcessBasically how does it all work2CMPE12c Gabriel Hugh Elkaim• A computer understands machine code - binary• People (and compilers) write assembly languageAssemblerAssemblycodeMachine codeThe Assembly Process3CMPE12c Gabriel Hugh ElkaimThe Assembly Process An assembler is a program that translates each instruction to its binary machine code equivalent.•It is a relatively simple program•There is a one-to-one or near one-to-one correspondence between assembly language instructions and machine language instructions.•Assemblers do some code manipulation•Like MAL to TAL•Label resolution•A “macro assembler” can process simple macros likeputs, or preprocessor directives.4CMPE12c Gabriel Hugh ElkaimMAL Æ TAL MAL is the set of instructions accepted by the assembler.TAL is a subset of MAL – the instructions that can be directly turned into machine code.•There are many MAL instructions that have no single TAL equivalent.•To determine whether an instruction is a TAL instruction or not:•Look in appendix C or on the MAL/TAL sheet.•The assembler takes (non MIPS) MAL instructions and synthesizes them into 1 or more MIPS instructions.5CMPE12c Gabriel Hugh ElkaimMAL Æ TAL mul $8, $17, $20For exampleBecomes•MIPS has 2 registers for results from integer multiplication and division: HI and LO•Each is a 32 bit register•mult and multu places the least significant 32 bits of its result into LO, and the most significant into HI.•Multiplying two 32-bit numbers gives a 64-bit result•(232– 1)(232– 1) = 264– 2x232-1mult $17, $20mflo $86CMPE12c Gabriel Hugh ElkaimMAL Æ TAL mflo, mtlo, mfhi, mthiMove From lo Move To hi•Data is moved into or out of register HI or LO•One operand is needed to tell where the data is coming from or going to.•For division (div or divu)•HI gets the remainder•LO gets the dividend•Why aren’t these just put in $0-$31 directly?7CMPE12c Gabriel Hugh ElkaimMAL Æ TAL TAL has only base displacement addressingSo this:lw $8, labelBecomes:la $7, labellw $8, 0($7)Which becomeslui $8, 0xMSPART of labelori $8, $8, 0xLSpart of labellw $8, 0($8)8CMPE12c Gabriel Hugh ElkaimMAL Æ TAL Instructions with immediate values are synthesized with other instructionsSo: add $sp, $sp, 4Becomes: addi $sp, $sp, 4For TAL:•add requires 3 operands in registers.•addi requires 2 operands in registers and one operand that is an immediate.•In MIPS assembly immediate instructions include:•addi, addiu, andi, lui, ori, xori•Why not more?9CMPE12c Gabriel Hugh ElkaimMAL Æ TAL TAL implementation of I/O instructionsThis:putc $18 # if you got to use macrosBecomes:addi $2, $0, 11 # code for putcadd $4, $18, $0 # put character argument in $4syscall # ask operating system to do a function10CMPE12c Gabriel Hugh ElkaimMAL Æ TAL getc $11Becomes:addi $2, $0, 12syscalladd $11, $0, $2puts $13Becomes:addi $2, $0, 4add $4, $0, $13syscalldoneBecomes:addi $2, $0, 10syscall11CMPE12c Gabriel Hugh ElkaimMAL Æ TAL add $4, $3, $0move $4, $3Arithmetic Instructions:div $9, $10mfhi $8rem $8, $9, $10div $9, $10 # $LO Å quotient# $HI Å remaindermflo $8div $8, $9, $10mult $9, $10 #HI –– LO Å product# never overflowmflo $8 # $8 Å $L0, ignore $HI!mul $8, $9, $10addi $4, $3, 15 # also andi, ori, ..add $4, $3, 15TALMAL12CMPE12c Gabriel Hugh ElkaimMAL Æ TAL bltz, bgez, blez, bgtz, beq, bnebltz, bgez, blez, bgtz, beqz, bnez, blt, bge, bgt, beq, bneslt $t0, $4, $5 # $t0 is 1 if $4 < $5# $t0 is 0 otherwisebne $t0, $0, targetblt $4, $5, targetbeq $4, $0, loopbeqz $4, loopBranch Instructions:TALMAL13CMPE12c Gabriel Hugh ElkaimAssembler The assembler will:•Assign addresses•Generate machine codeIf necessary, the assembler will:•Translate (synthesize) from the accepted assemblyto the instructions available in the architecture•Provide macros and other features•Generate an image of what memory must look like forthe program to be executed.14CMPE12c Gabriel Hugh ElkaimAssembler What should the assembler do when it sees a directive?• .data• .text• .space, .word, .byte, .float• main:How is the memory image formed?15CMPE12c Gabriel Hugh ElkaimAssembler Example Data Declaration•Assembler aligns data to word addresses unless told not to.•Assembly process is very sequential..dataa1: .word 3a2: .byte ‘\n’a3: .space 5Address Contents0x00001000 0x000000030x00001004 0x??????0a0x00001008 0x????????0x0000100c 0x????????16CMPE12c Gabriel Hugh ElkaimMachine code generation •opcode is 6 bits – addi is defined to be 001000•rs – source register is 5 bits, encoding of 20, 10100•rt – target register is 5 bits, encoding of 8, 01000The 32-bit instruction for addi $8, $20, 15 is:001000 10100 01000 0000000000001111Or0x2288000fAssembly language: addi $8, $20, 15opcodert rsimmediate31 0opcode rs rt immediateMachine code format:17CMPE12c Gabriel Hugh ElkaimInstruction Formats I-Type Instructions with 16-bit immediates•ADDI, ORI, ANDI, …•LW, SW•BNEOPC:6 rs1:5 rd:5 immediate:16OPC:6 rs1:5 rs2/rd displacement:16OPC:6 rs1:5 rs2:5 distance(instr):1618CMPE12c Gabriel Hugh ElkaimInstruction Formats J-Type Instructions with 26-bit immediate•J, JALR-Type All other instructions•ADD, AND, OR, JR, JALR, SYSCALL, MULT, MFHI,LUI, SLTOPC:6 26-bits of jump addressOPC:6 rs1:5 rs2:5 ALU function:11rd:519CMPE12c Gabriel Hugh ElkaimAssembly Example .dataa1: .word 3a2: .word 16:4a3: .word 5.textmain:la $6, a2loop: lw $7, 4($6)mul $8, $9, $10b loopdone“Symbol Table”0080 0008loop0080 0000main0040 0014a30040 0004a20040 0000a1AddressSymbol20CMPE12c Gabriel Hugh ElkaimAssembly Example 0000 0000 0000 0000 0000 0000 0000 01010000 00050040 00140000 0000 0000 0000 0000 0000 0001 00000000 00100040 00100000 0000 0000 0000 0000 0000 0001 00000000 00100040 000c0000 0000 0000 0000 0000 0000 0001 00000000 00100040 00080000 0000 0000 0000 0000 0000 0001 00000000 00100040 00040000 0000 0000 0000 0000 0000 0000 00110000 00030040 0000Contents (binary)Contents (hex)addressMemory map of .data section21CMPE12c Gabriel Hugh ElkaimAssembly Example Translation of MAL to TAL code.textmain: lui $6, 0x0040 # la $6, a2ori $6, $6, 0x0004loop: lw $7, 4($6)mult $9, $10 # mul $8, $9, $10mflo $8beq $0, $0, loop # b loopori $2, $0, 10 # donesyscall22CMPE12c Gabriel Hugh Elkaim0000 0000 0000 0000 0100 0000 0001 0010 (mflo)0000 40120080 00100011 0100 0000 0010 0000 0000 0000 1010 (ori)3402 000a0080 00180000 0000 0000 0000 0000 0000 0000 1100 (sys)0000


View Full Document

UCSC CMPE 012 - The Assembly Process

Download The Assembly Process
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 The Assembly Process 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 The Assembly Process 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?