
SPARC Assembly Language ! Load/Store architectures ! Machine code, assembly, and high level languages ! The SPARC assembly language "Structure of an assembly program "SPARC register pool "Directives "Basic instructions The SPARC Assembly Language "Instruction pipelining in the SPARC architecture "Branching instructions "mapping of control flow instructions to assembly language Prof. Gustavo Alonso Computer Science Department ETH Zürich [email protected] http://www.inf.ethz.ch/department/IS/iks/ ©Gustavo Alonso, ETH Zürich. Programming in Assembly 2 Load/Store architectures Machine code ! Processors are just a complex ! Machine code is the binary collection of digital gates and registers representation of the instructions processing electronic signals that can understood by the processor (by the be interpreted in binary form: 1 or 0 instruction decoder in the processor) ! The instruction decoder takes an ! How the machine code is processed instruction and generates (how it does determines many characteristics of the not matter) the necessary signals to processor: perform operations across the different "CISC (complex instruction set components: computer): the machine code "load data (bring some data into a contains many different register) operations, including complex ones "store data (move data from a (e.g., matrix multiplication). The register to somewhere else) execution often involves executing "operate on data (add, shift, “subroutines” (micro-code) compare, etc.) "RISC (reduced instruction set ! When the operations are done on a set computer): machine code contains of registers rather than directly in only instructions that can be memory, the architecture is called a quickly executed. The number of load/store architecture instructions is small (reduced) ©Gustavo Alonso, ETH Zürich. Programming in Assembly 3 ©Gustavo Alonso, ETH Zürich. Programming in Assembly 4 Assembly language High level programming languages ! ! Machine code is binary and, therefore, .data Advantages of assembly language: main() unsuitable for direct manipulation by a: .word 1 "very efficient { humans b: .word 2 "allows to manipulate the hardware int a = 1; ! To program at the machine code level, int b = 2; start: .text almost directly (necessary for int c; one uses an assembly language. The set a, %r1 writing drivers and low level c = a + b; assembly language is simply a textual ld [%r1], %r1 components of the operating printf("%d\n",c); representation of machine code plus set b, %r2 system) } some syntactic rules that can be ld [%r2], %r2 interpreted by the assembler. The add %r1, %r2, %r3 ! Disadvantages of assembly language: .data assembler is the program that takes end: ta 0 "machine dependent (the language a: .word 1 assembly code as input and produces works on that processor and b: .word 2 nowhere else) machine code as output ^A^C^A^K \234 start: .text ^C^P\202^P`^DÂ@^E^P\204^ "programs are cumbersome and set a, %r1 ! Assembly code is closely tied to the P Ä\200\206X@^B\221D underlying processor architecture. Its ^Oÿ^Oÿ^D^C^N^D cryptic ld [%r1], %r1 ^U^F@^X^F@^D^[^D !^D set b, %r2 basic instruction set is the machine <%^E @-^E @4 @^H:^G@^HB "very repetitive programs ld [%r2], %r2 code instruction set of the processor. @^HN ! Higher level languages try to solve add %r1, %r2, %r3 Each assembler adds a dialect the these problems by providing better end: ta 0 programmer can use to build real abstractions programs in assembly language ©Gustavo Alonso, ETH Zürich. Programming in Assembly 5 ©Gustavo Alonso, ETH Zürich. Programming in Assembly 6 A toy assembly language Structure of an assembly program ! LOADA mem - Load register A from memory address mem Assembly language programs are line-oriented (the assembler translates an assembly LOADB mem - Load register B from memory address mem program one line at a time). The assembler recognizes four types of lines: empty CONB con - Load a constant value into register B lines, label definition lines, directive lines, and instruction lines. SAVEB mem - Save register B to memory address mem "A line that only has spaces or tabs (i.e., white space) is an empty line. Empty SAVEC mem - Save register C to memory address mem lines are ignored by the assembler. ADD - Add register A and register B and store the result in register C SUB - Subtract register A and register B and store the result in register C "A label definition line consists of a label definition. A label definition consists of MUL - Multiply register A and register B and store the result in register C an identifier followed by a colon (“:”). As in most programming languages, an DIV - Divide register A and register B and store the result in register C identifier must start with a letter (or an underscore) and may be followed by any COM - Compare register A and register B and store result in register test number of letters, underscores, and digits. JUMP addr - Jump to address addr JEQ addr - Jump if the previous comparison was equal (register test is 0), to address addr "A directive line consists of an optional label definition, followed by the name of JNEQ addr - Jump if the previous comparison was not equal (register test is 0), to address an assembler directive, followed by the arguments for the directive. addr "An instruction line consists of an optional label definition, followed by the name JG addr - Jump if the comparison is Greater than (result is in register test), to address addr of an operation, followed by the operands. JGE addr - Jump if Greater than or equal (result is in register test), to address addr JL addr - Jump if Less than (result is in register test), to address addr ! Comments within a line begin with the character “!”. C-style type of comments JLE addr - Jump if Less than or equal (result is in register test), to address addr (spanning several lines) are allowed using /* … */ STOP - Stop execution ©Gustavo Alonso, ETH Zürich. Programming in Assembly 7 ©Gustavo Alonso, ETH Zürich. Programming in Assembly 8 Segments and statements Assembly program (example 1) ! An assembly program, is organized in ! Internally, machine code is binary and .data ! variables three segments: it is processed in binary form a: .word 0x42 ! a initialized to 0x42 b: .word 0x43 ! b initialized to 0x43 "data segment: constants and data ! In assembly, one can work with c: .word 0x44 ! c initialized to 0x44 necessary for the program different systems: d: .word 0x45 ! d initialized to 0x45 "text segment: the instructions of "hexadecimal (0x…) .text ! Instructions a = (a+b) - (c-d) the program " start: set a, %r1 octal (0…) ld [%r1], %r2 ! $a$ --> %r2 "BSS segment: (Block Storage "decimal set b, %r1 Segment or Block Started by ld [%r1], %r3 ! $b$ --> %r3 Symbol) space for dynamic data ! Later on we will discuss these different set c, %r1 systems. However, keep in mind that ld [%r1], %r4 ! $c$ --> %r4 and non initialized global variables we will use hexadecimal and octal more set d, %r1 ! An statement: often than decimal ld [%r1], %r5 ! $d$ --> %r5 label: instruction ! add %r2, %r3, %r2 ! $a+b$ --> %r2 label: Also keep in mind that load and store sub %r4, %r5, %r3 ! $c-d$ --> %r3 instruction architectures are register based. Most sub %r2, %r3, %r2 ! $(a+b)-(c-d)$ --> %r2 of the instructions involve set a, %r1 ! A label is a symbol or a single digit. manipulating one or more registers st %r2, [%r1] ! $(a+b)-(c-d)$ --> a An instruction is a pseudo-op (assembler directive), synthetic end: ta 0 instruction, or instruction. ©Gustavo Alonso, ETH Zürich. Programming in Assembly 9 ©Gustavo Alonso, ETH Zürich. Programming in Assembly 10 SPARC register pool Directives ! The assembly language we will learn is ! Global registers are used for global ! The different sections of the program ! .byte value1, ..., valuen the assembly language of the SPARC V8 variables are marked with the following Represents a sequence of bytes, architecture (current V9) "%r0 is an special register that directives (also called pseudo- initialized with the given data, in always holds the value 0 and operations): ! This is a RISC architecture with 32 cannot be modified sequence. The values must fit integer registers. Each integer register ".text for the program code, within 8 bits each ! Output registers are used for local data holds 32-bits. The integer registers are and arguments to/from subroutines ".data for the global writeable ! .halfword value1, ..., valuen called %r0 through %r31. In addition to "%r14 (%sp, %o6) is the stack initialized data, Represents a sequence of halfwords, the names %r0 through %r31, the pointer " integer registers have alternative names .bss for the global uninitialized initialized with the given data, in "%r15 is the return address of the data sequence. The values must fit "global registers (%g0-%g7) called subroutine within 16 bits each correspond to registers %r0-%r7 ! .ascii string1, ..., stringn ! Local registers are for general use ! .word value1, ..., valuen " (local variables) Represents a sequence of bytes, output registers (%o0-%o7) initialized with the ASCII Represents a sequence of words, correspond to registers %r8-%r15 ! Input registers are used for argument passing from subroutines encoding of the strings, in initialized with the given data, in "local registers (%l0-%l7) sequence, without string sequence. The values must fit correspond to registers %r16-%r23 "%r30 (%fp, %i6) is the frame pointer terminators (.asciz adds \0) within 32 bits each "input registers (%i0-%i7) "%r31 is the subroutine return ! .global label ! .include “file_name” correspond to registers %r24-%r31 address Makes a label global Used to add additional definitions from other files ©Gustavo Alonso, ETH Zürich. Programming in Assembly 11 ©Gustavo Alonso, ETH Zürich. Programming in Assembly 12 Basic instructions (1) Basic instructions (2) Program begin set load move ! The beginning of a program is indicated ! The set operation allows to load a ! The load instruction brings a word from ! The mov (move) instruction copies the with the .text directive.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages8 Page
-
File Size-