ta6 2006 Amar Lior Based on lectures notes from Arie - - PDF document

ta6 2006 amar lior based on lectures notes from arie
SMART_READER_LITE
LIVE PREVIEW

ta6 2006 Amar Lior Based on lectures notes from Arie - - PDF document

ta6 2006 Amar Lior Based on lectures notes from Arie Schlesinger (aries@cs.columbia.edu) Adapted from Computer Organization&Design, H/S interface, Patterson Hennessy@UCB,1999 1 Communicating with People As soon


slide-1
SLIDE 1

1

1

הנבמ םיבשחמ

ta6

2006 Amar Lior

Based on lectures notes from Arie Schlesinger (aries@cs.columbia.edu)

Adapted from Computer Organization&Design, H/S interface, Patterson Hennessy@UCB,1999 2

Communicating with People

As soon as computers became commercial

they were used to process text

Most use 8-bit bytes to represent characters Using the American Standard Code for

Information Interchange (ASCII)

MIPS provide instructions to move bytes

lb $t0, 0($sp)

# Read byte from source

sb $t0, 0($gp)

# Write byte to destination

3

Strcpy example

strcpy: addi $sp, $sp, -4 sw $s0, 0($sp) add $s0, $zero, $zero L1: add $t1, $s0, $a1 lb $t2, 0($t1) add $t3, $s0, $a0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1 L2: lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

void strcpy (char x[], char y[]) { int i; i=0; while ((x[i] = y[i]) != ‘\0’) i++; }

slide-2
SLIDE 2

2

4

Strings in java

Java uses Unicode for characters By default, it uses 16 bit to represent a character The MIPS instructions has explicit instructions to

load and store such 16-bit quantities

Called halfword

  • lh $t0, 0($sp)
  • sh $t0, 0($sp)

Unlike C, Java includes a word that gives the length

  • f the string

5

Translating and Starting a Program

Compiler Assembly Language Program C program Assembler Object: Machine language module Linker Executable: Machine language program Object: Machine language module Loader Memory

6

The output of the assembler

The assembler must determine the addresses

corresponding to all labels

A symbol table is used to keep track of labels used

for branches and data transfer

The object file typically contains:

Object file header : describes size and position of other

parts

Text segment Static data segment Relocation information: identifies instructions and data

words that depend on absolute addresses

Symbol table : contains the remaining labels that are not

defined

Debugging information

slide-3
SLIDE 3

3

7

Linker

Single change to one line of one procedure requires

compiling and assembling the whole program

An alternative is to compile and assemble each procedure

independently

This requires a new system program called link editor or

linker

Work in 3 steps

Place code and data modules symbolically in memory Determine the addresses of data and instructions labels Patch both internal and external references

Uses the relocation information and symbol table to resolve

all undefined labels

The reason for the linker is that patching the code is much

faster than compiling and reassemble

8

Linker

When all external references are resolved The linker next determines the memory location

each module will occupy

Since the files are assembled in isolation the assembler could not know where a module’s

instruction and data will be placed relative to

  • ther modules

When the module is placed in memory all absolute

references must be relocated to reflect its true location

The linker produce an executable file that can be

run on the computer

9

Linker example

  • B
  • X

Address Label Symbol Table B jal 4 X lw dep Instruction type Address Relocation Info … … (X) Data Segment … … jal 0 4 lw $a0, 0(gp) Instruction Address Text segment 20hex Data Size 100hex Text Size Procedure A Name Object Header

slide-4
SLIDE 4

4

10

Linker example

  • A
  • Y

Address Label Symbol Table A jal 4 Y sw dep Instruction type Address Relocation Info … … (Y) Data Segment … … jal 0 4 sw $a1, 0(gp) Instruction Address Text segment 30hex Data Size 200hex Text Size Procedure B Name Object Header

11

Linker example

Adress Data segment Text segment Executable file header … … … … (Y) 1000 0020 … … (x) 1000 0000 … … Jal 40 000 0040 0104 Sw $a1, 8020($gp) 0040 0100 Jal 40 0100 0040 0004 Lw $a0, 8000($gp) 0040 0000 Instruction Address 50hex Data Size 300hex Text Size

12

Object symbols example

File: t.c int main() { f(1); } int g(void) { return 1; } File: t1.c int f(void) { return 1; }

mos215:~> nm t.o U f 0000002b T g 00000000 T main mos215:~> nm t1.o 00000000 T f

slide-5
SLIDE 5

5

13

The symbols in the final binary

mos215:~> nm ttt 080494f0 D _DYNAMIC 080495bc D _GLOBAL_OFFSET_TABLE_ 080484d4 R _IO_stdin_used w _Jv_RegisterClasses 080495dc A __bss_start 080495d0 D __data_start 08048480 t __do_global_ctors_aux 08048300 t __do_global_dtors_aux 080495d4 D __dso_handle 080494dc A __fini_array_end 080494dc A __fini_array_start 080495dc A _edata 080495e0 A _end 0804825c T _init 080482b0 T _start 080495d0 W data_start 080483a0 T f 08048340 t frame_dummy 08048393 T g 08048368 T main 14

Another symbol example

.data hw: .string "hello world\n" .text .global main main: movl $4, %eax movl $1, %ebx movl $hw, %ecx movl $12, %edx int $0x80 movl $1, %eax xorl %ebx, %ebx int $0x80 ret

mos215:~/teach/comarc/linux-asm> nm hello 08049494 D _DYNAMIC 08049570 D _GLOBAL_OFFSET_TABLE_ 08048470 R _IO_stdin_used w _Jv_RegisterClasses 08049584 A __bss_start 08049474 D __data_start 08048420 t __do_global_ctors_aux 08048410 T __i686.get_pc_thunk.bx 080483c0 T __libc_csu_fini 08048360 T __libc_csu_init U __libc_start_main@@GLIBC_2.0 08049584 A _edata 08049588 A _end 0804822c T _init 08048270 T _start 08048294 t call_gmon_start 08049474 W data_start 08049480 d hw 08048334 T main 0804947c d p.0 15

Loader

The Operating System read the executable file from the disk

to memory and starts it

Read the executable file header and determine size of text and data

segment

Create an address space large enough for the text and data Copies the instructions and data Copies the parameters (if any) of the main program to the stack Initialize the machine registers and set the stack pointer to the first

free location

Jumps to a startup routine that copies the parameters into the

argument registers

When the main routine returns, the startup routines call the exit

system call

slide-6
SLIDE 6

6

16

Dynamically Linked Libraries

Statically linked executables has several

disadvantages:

The library routine become part of the executable code It loads the whole library even if the code is not used

when the program is run (can be very big)

Using dynamically linked libraries the library

routines are not linked and loaded until the program is run

In the initial versions of DLLs, the loader run a

dynamic linker

It uses extra information in the file to find the

appropriate library and to update all external references

17

DLL’s

A downside of the pervious version?? Lazy procedure linking the routine is linked only

after it is called

This uses a level of indirection There is a dummy routine for each external

procedure

This dummy routine call a dynamic linker-loader

routine with the id of the routine to load

The linker-loader also change the indirect jump to

point directly to the routine

So further callas does not access the linker-loader

18

Example: calling a DLL routine

Data Text

Li IDl j

Text

Dynamic Linker Loader j

Text/data

DLL Routine … Jr

Text

Jal … lw jr

First call to DLL routine

slide-7
SLIDE 7

7

19

Example: calling a DLL routine

Data Text

Jal … lw jr

Text/data

DLL Routine … Jr

Subsequent calls to DLL routine

20

Tools in Unix

mos215:~> ldd /bin/ls linux-gate.so.1 => (0xffffe000) librt.so.1 => /lib/tls/i686/cmov/librt.so.1 (0xb7f34000) libacl.so.1 => /lib/libacl.so.1 (0xb7f2e000) libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7df6000) libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0xb7de4000) /lib/ld-linux.so.2 (0xb7f5c000) libattr.so.1 => /lib/libattr.so.1 (0xb7de0000) mos215:~/teach/comarc/linux-asm> ldd /usr/bin/xspim linux-gate.so.1 => (0xffffe000) libXaw.so.7 => /usr/X11R6/lib/libXaw.so.7 (0xb7e8e000) libXmu.so.6 => /usr/X11R6/lib/libXmu.so.6 (0xb7e79000) libXt.so.6 => /usr/X11R6/lib/libXt.so.6 (0xb7e29000) libSM.so.6 => /usr/X11R6/lib/libSM.so.6 (0xb7e20000) libICE.so.6 => /usr/X11R6/lib/libICE.so.6 (0xb7e09000) libXpm.so.4 => /usr/X11R6/lib/libXpm.so.4 (0xb7df3000) libXext.so.6 => /usr/X11R6/lib/libXext.so.6 (0xb7de5000) libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0xb7d1a000) libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7cf4000) libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7bbd000) libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0xb7bb9000) /lib/ld-linux.so.2 (0xb7f07000) mos215:~/teach/comarc/linux-asm>