Вы находитесь на странице: 1из 18

Lecture 14

Compilers, Assemblers, Linkers and XSPIM Programming

14.1

Compilers are for High-Level Languages


High-level language program Program Compiler Assembler Linker Computer

Assembly language program

14.2

A Separately Compiled File

Object file header

Text segment

Data segment

Relocation information

Symbol table

Debugging information

14.3

Piecing Together an Executable from Separately Compiled Units


Object file sub: Object file Instructions main: jal ??? jal ??? call, sub call, printf Linker Executable file main: jal printf jal sub printf: sub:

Relocation records

C library print:

14.4

Piecing Together Separately Assembled Units


Source file Assembler Object file

Source file

Assembler

Object file

Linker

Executable file

Source file

Assembler

Object file

Program library

14.5

Why Program in Assembly?


If what you need is to have mode direct control of the machine... P&H Excellent Advice: Stick to high-level language (if you have a good compiler) as much as you possibly can.
14.6

xspim
PC = 00000000 Status = 00000000 R0 R1 R2 R3 R4 R5 R6 R7 FP0 FP2 FP4 FP6 (r0) (at) (v0) (v1) (a0) (a1) (a2) (a3) = = = = = = = = = = = = 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0.000000 0.000000 0.000000 0.000000 EPC HI R8 R9 R10 R11 R12 R13 R14 R15 FP8 FP10 FP12 FP14 = 00000000 = 00000000 (t0) (t1) (t2) (t3) (t4) (t5) (t6) (t7) = = = = = = = = = = = = Cause LO = 00000000 BadVaddr = 00000000 R16 R17 R18 R19 R20 R21 R22 R23 FP16 FP18 FP20 FP22 (s0) (s1) (s2) (s3) (s4) (s5) (s6) (s7) = = = = = = = = = = = = 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0.000000 0.000000 0.000000 0.000000 = 00000000

General registers
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0.000000 0.000000 0.000000 0.000000 R24 R25 R26 R27 R28 R29 R30 R31 FP24 FP26 FP28 FP30 (t8) (s9) (k0) (k1) (gp) (sp) (s8) (ra) = = = = = = = = = = = = 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0.000000 0.000000 0.000000 0.000000

Register display

Double floating-point registers

Single floating-point registers Control buttons quit print load breakpt run help step terminal Text segments
[0x00400000] [0x00400004] [0x00400008] [0x0040000c] [0x00400010] [0x00400014] [0x00400018] [0x0040001c] 0x8fa40000 0x27a50004 0x24a60004 0x00041080 0x00c23021 0x0c000000 0x3402000a 0x0000000c lw $4, 0($29) addiu $5, $29, 4 addiu $6, $5, 4 sll $2, $4, 2 addu $6, $6, $2 jal 0x00000000 [main] ori $2, $0, 10 syscall ; ; ; ; ; ; ; ; 89: 90: 91: 92: 93: 94: 95: 96: lw $a0, 0($sp) addiu $al, $sp, 4 addiu $a2, $al, 4 sll $v0, $a0, 2 addu $a2, $a2, $v0 jal main li $v0 10 syscall

clear mode

set value

Text segments

Data segments
[0x10000000] ... [0x10010000] [0x10010004] 0x74706563 [0x10010010] 0x72727563 [0x10010020] 0x000a6465 [0x10010030] 0x0000205d [0x10010040] 0x61206465 [0x10010050] 0x642f7473 [0x10010060] 0x555b2020 [0x10010070] 0x73736572 0x00000000 0x206e6f69 0x61206465 0x495b2020 0x20200000 0x65726464 0x20617461 0x696c616e 0x206e6920 0x636f2000 0x6920646e 0x7265746e 0x616e555b 0x69207373 0x63746566 0x64656e67 0x726f7473

Data and stack segments

0x726f6e67 0x74707572 0x6e67696c 0x6e69206e 0x00205d68 0x64646120 0x00205d65

SPIM messages

SPIM Version 5.9 of January 17, 1997 Copyright (c) 19901997 by James R. Larus (larus@cs.wisc.edu) All Rights Reserved. See the file README for a full copyright notice.

14.7

Memory Image of an Executing Program


7fffffffhex Stack segment

Dynamic data 10000000hex 400000hex Static data

Data segment Text segment

Reserved
14.8

Quick Detour: The 80x86 Register Set


Name EAX ECX EDX EBX ESP EBP ESI EDI CS SS DS ES FS GS EIP EFLAGS 31 0 Use GPR 0 GPR 1 GPR 2 GPR 3 GPR 4 GPR 5 GPR 6 GPR 7

These registers allow the programmer to refer to memory locations with base+offset addressing.

Code segment pointer Stack segment pointer (top of stack) Data segment pointer 0 Data segment pointer 1 Data segment pointer 2 Data segment pointer 3 Instruction pointer (PC) Condition codes

14.9

Assembler Directives
Data alignment: .align n (n is a power of 2) - by default, auto alignment is on for word, half-word, float and double Data segment definition: .data <addr>
.data MY_VAR1: .float 3.1416936 GREET: .asciiz: Welcome to my first MIPS program DUDE: .word 0xdada B_ARRAY: .byte 32, 12, 23, 3, 35, 76, 99

Code segment definition: .text <addr>


.text addi sub START: sub bne $t0, $zero, 64 $t1, $zero, 1 $t0, $t0, $t1 $t0, $zero, START
14.10

10

Assembler Directives
Comments:
# this is a comment

Address labels:
addi $t0, $zero, 64 sub $t1, $zero, 1 START: sub bne $t0, $t0, $t1 $t0, $zero, START

14.11

11

Assembler Directives
Space allocation: (in the data segment)
MY_EMPTY_ARRAY: .space n

Please refer to PH2 page A-51 for the comprehensive list.

14.12

12

Services
If you write code for a bare machine, your program must be a self-contained unit. All the functionality you require must be provided by your own code. If you write code to run on a machine with an operating system, you can make use of the (possibly many) services offered. First, you must figure out how to invoke these services, what parameters need to passed and how you pass these parameters. XSPIM doesnt fall in either of these cases, but it offers you a few services that can make your life easier.
14.13

13

XSPIM Services
Service
print_int print_float print_double print_string read_int read_float read_double read_string sbrk exit

Code
1 2 3 4 5 6 7 8 9 10

Arguments
$a0 = integer $f12 = float $f12 = double $a0 = string

Result

integer (in $v0) float (in $f0) double (in $f0) $a0 = buffer, $a1 = length $a0 = amount address (in $v0)

14.14

14

XSPIM Startup
lw addiu addiu sll addu jal li syscall $a0, $a1, $a2, $v0, $a2, main $v0, 0($sp) #argc $sp, 4 #argv $a1, 4 #envp $a0, 2 $a2, $v0 10 # syscall 10 (exit)

Your program should better define a main symbol because XSPIM will look for it.
14.15

15

Recursive Procedures
main( ) { printf(10! = %d\n, fact(10); } int fact(int n) { if (n<1) return (1); else return (n*fact(n-1)); }
14.16

16

A View of the Stack


Stack Old $ra Old $fp Old $ra Old $fp Old $a0 Old $ra Old $fp Old $a0 Old $ra Old $fp Old $a0 Old $ra Old $fp Old $a0 main

fact (10)

fact (9)

fact (8) Stack grows


14.17

fact (7)

17

.text main: subu sw sw addiu li jal lw lw addiu jr .data $LC: .asciiz

.text
fact:

$sp, $sp, 32 $ra, 24($sp) $fp, 16($sp) $fp, $sp, 28 $a0, 10 fact $ra, 24($sp) $fp, 16($sp) $sp, $sp, 32 $ra

$L2:

$L1:

10! =

subu sw sw addiu sw lw bgtz li j lw subu move jal lw mul lw lw addu jr

$sp, $sp, 32 $ra, 24($sp) $fp, 16($sp) $fp, $sp, 28 $a0, 0($fp) $v0, 0($fp) $v0, $L2 $v0, 1 $L1 $v1, 0($fp) $v0, $v1, 1 $a0, $v0 fact $v1, 0($fp) $v0, $v0, $v1 $ra, 24($sp) $fp, 16($sp) $sp, $sp, 32 $ra

14.18

18

Вам также может понравиться