chap9

14
Chapter 9 Chapter 9 External Data & Text External Data & Text Lecture Notes for SPARC Lecture Notes for SPARC Architecture Architecture , , Assembly language Assembly language programming and C programming and C , Richard P. Paul , Richard P. Paul By Ken Nguyen By Ken Nguyen

Upload: jack-blair

Post on 14-Dec-2015

212 views

Category:

Documents


0 download

DESCRIPTION

SPRAC/Assembly tutorial

TRANSCRIPT

Page 1: chap9

Chapter 9 Chapter 9 External Data & TextExternal Data & Text

Lecture Notes for SPARC Lecture Notes for SPARC ArchitectureArchitecture, , Assembly language Assembly language

programming and Cprogramming and C, Richard P. Paul, Richard P. Paul

By Ken NguyenBy Ken Nguyen

Page 2: chap9

External VariablesExternal Variables

►► There are two types of external variables:There are two types of external variables:Zero initialized.Zero initialized.NonNon--zero initialized.zero initialized.

When a program is loaded into memory, the program text, When a program is loaded into memory, the program text, initialized variable, and zero initialized variables are initialized variable, and zero initialized variables are loaded into different regions (loaded into different regions (texttext, , data,data, and and bssbsssections, respectively.) of memory, typically starting at sections, respectively.) of memory, typically starting at 0x2000 byte boundary.0x2000 byte boundary.

Page 3: chap9

Text SectionText Section

►►Text Text section: Read only memory region, section: Read only memory region, where the machine codes go.where the machine codes go.

Addresses of machine instructions are relative Addresses of machine instructions are relative to the beginning of the program.to the beginning of the program.The beginning of the program is signified by The beginning of the program is signified by label label mainmain, usually made as global., usually made as global.Calls/branches in a program are counter Calls/branches in a program are counter relative. (relative to PC.)relative. (relative to PC.)

Page 4: chap9

Data SectionData Section►►Data Data section: Read/Write memory region, section: Read/Write memory region,

where nonwhere non--zero variables go.zero variables go.Variables are specified by size, like: Variables are specified by size, like: double double wordword, , word, half word word, half word andand byte. byte. Variables can be aligned on boundary using Variables can be aligned on boundary using .align .align String/Character type can be represented in String/Character type can be represented in three different ways: three different ways: .byte.byte, , ..asciiascii, and , and ..ascizasciz, , where where ..ascizasciz gives a null terminated string.gives a null terminated string.

►►Variables must be initialized.Variables must be initialized.

Page 5: chap9

►►Example:Example:A) A)

ArrayArray: .word 0, 1, 2, 3, 4, 5: .word 0, 1, 2, 3, 4, 5B)B)

NoMemNoMem: : .byte.byteArray2Array2: : .byte 1, 2, 3, 4 .byte 1, 2, 3, 4 ⇒⇒NoMemNoMem has the same address as has the same address as Array2Array2

Page 6: chap9

BSS SectionBSS Section

►►BSSBSS section (section (BBlock lock SStarting tarting SSymbol): ymbol): Read/Write memory region, where zero Read/Write memory region, where zero initialized variables go.initialized variables go.

►►Memory allocation through skipping bytes.Memory allocation through skipping bytes.

Example:Example:array: .skip 4 * 100 ! Initialize 400 byte to 0array: .skip 4 * 100 ! Initialize 400 byte to 0

Page 7: chap9

►►.common.common usually used to make a variable usually used to make a variable in in BSSBSS global. Alignment can be provided.global. Alignment can be provided.

Example:Example:.common array2, 4*100, 4.common array2, 4*100, 4

⇒⇒Zero Zero 4*100 bytes4*100 bytes and keep the first byte and keep the first byte aligned by 4 (word boundary.)aligned by 4 (word boundary.)

Page 8: chap9

Accessing VariablesAccessing Variables►► Use set instruction to get the address of a variable into a Use set instruction to get the address of a variable into a

register.register.►► Use load or store instruction to read/write from/to the Use load or store instruction to read/write from/to the

variable.variable.►► ExampleExample.section .section ““.data.data””aa: .word 5: .word 5…….section .section ““.text.text””set set aa, %l0 !get the address of a to %lo, %l0 !get the address of a to %lold [%l0], %o0ld [%l0], %o0 !get the value of a into %o0!get the value of a into %o0What is the content of %o0 now????What is the content of %o0 now????

Page 9: chap9

Example cont.Example cont.

.section .section ““.data.data””bb: .byte 9: .byte 9…….section .section ““.text.text””set set bb, %l1, %l1 ! Load the address of ! Load the address of bb to %l1to %l1movmov 10, %l2 ! Move 10 to %l210, %l2 ! Move 10 to %l2stbstb %l2, [%l1]%l2, [%l1] ! Store value 10 into ! Store value 10 into bb

What is the value of b now????What is the value of b now????

Page 10: chap9

►►setset is a combination of two instructions: is a combination of two instructions: sethisethi and and oror. Therefore, do not use . Therefore, do not use setset in in delay slot.delay slot.

Example:Example:set set bb, %l1 , %l1

is equivalent tois equivalent tosethisethi %%hi(bhi(b), %l1), %l1or %l1, %or %l1, %lo(blo(b), %l1), %l1

Page 11: chap9

Switch statementSwitch statement

►►Covered in previous chaptersCovered in previous chapters

Page 12: chap9

Relocation and LinkingRelocation and Linking►► A program can be written in one or several A program can be written in one or several

source modules. source modules. ►► A label must be made global to be accessible A label must be made global to be accessible

from other source modules.from other source modules.►► Each section (Each section (text, data, text, data, bssbss) is loaded into ) is loaded into

memory at different locations at run time. memory at different locations at run time. Therefore, assembler uses address of a label as Therefore, assembler uses address of a label as an offset from the beginning of the section. an offset from the beginning of the section. When a section is loaded, the loader adds the When a section is loaded, the loader adds the starting address of the loading section to all starting address of the loading section to all references => no problem when any section is references => no problem when any section is relocated. relocated.

Page 13: chap9

Compilation Compilation

►►Multiple modules can be compiled and Multiple modules can be compiled and linked aslinked as

gccgcc a.sa.s b.sb.s c.sc.s ––oo projectprojectWhereWhere a.sa.s, , b.sb.s, , c.sc.s are source modulesare source modules

Page 14: chap9

Make & Make & makefilemakefile

►► Large project with multiple modules can be hard Large project with multiple modules can be hard to keep track of all dependencies and updates => to keep track of all dependencies and updates => makemake ultilityultility is used, instead.is used, instead.

►► makemake [[––f f filename] filename] filenamefilename is a file with the following formatis a file with the following format

Target:Target: dependent modulesdependent modulescommand command (first character is a (first character is a tabtab i.ei.e \\tt))

Example: Example: projproj: : project.sproject.s a.sa.s b.sb.s c.sc.s

gccgcc ––o o projproj project.sproject.s a.sa.s b.sb.s c.sc.s