Fichas
Week 5
Fichas del conjunto (49)
- 1machine languageThe bits representing an instruction (written in hex)
The hardware runs the machine language
specify the program in complete detail
– The program is words containing 16-bit numbers: 042c
– It's possible for a digital circuit (the computer) to execute
– No names for instructions or variables: everything is a number - 2Assembly languageAssembly language is for humans, machine language is for machines
specify the program in complete detail
– The program is text: add R4,R2,R12
– It's easier to read
– You don't need to remember all the codes
– Memory addresses are much easier to handle (e.g. x) - 3What's in the memory• All your program's data
– Variables
– Data structures, arrays, lists
• And also the machine language program itself - 4stored program computerThe program is stored inside the computer's main memory with the data
Alternative approach: have a separate memory to hold the program
(inferior for general purpose, special purpose computers often do this) - 5Sigma16 has three kinds of instruction– RRR instructions use the registers
– RX instructions use the memory
– EXP instructions use registers and constant - 6instruction format• Each kind of instruction is called an instruction format
– All the instructions with the same format are similar - 7Fields of an instruction word• An instruction word has 16 bits
• There are four fields, each 4 bits
• We write the value in each field using hexadecimal
• The fields have standard names
op/d/a/b - 8opholds the operation code
- 9dusually holds the destination register
- 10ausually holds the first source operand register
- 11busually holds the second source operand register
- 12RRR instructions– Typical example: add R8,R2,R12
– R8 is the destination (where the result goes)
– R2 and R12 are the sources (the operands to be added)
Total requirements: 4 fields, each 4 bits (total 16 bits)
Three register operands: a destination and two operands - 13RX instructions– RX instructions specify a register and a memory location
– Example: load R3,x[R0], means R3 = x
– Another example: store R3,y[R0], means y = R3
For RX the op field always contains f
– load copies from memory to register
– store copies from register to memory - 14operation codeWe specify which RRR instruction is with the operation code (a number)
– There are 14 RRR instructions, so a 4-bit operation code is enough - 15RRR three registers– There are 16 registers, so a particular one can be specified by 4 bits
- 16memory addressx[R0]
– The variable x is the address where x is kept, called the displacement
– The R0 part is just a register, called the index register - 17address of a variableEach variable is kept in memory at a specific location
- 18assemblertranslates it to machine language
instruction mnemonic replaces it with the operation code
assembler helps with variable names: the machine language needs
addresses (numbers) and the assembler calculates them - 19Compilers vs Assembler– Compilers translate between languages that are very different
– Assemblers translate between very similar languages - 20location counter– This is the address where it will place the next piece of code
– Initially the location counter is 0 - 21symbol tableIf there is a label, it remembers that the address of the label is the current value of the location counter
- 22How the assembler allocates memoryThe assembler maintains a variable called the location counter
The assembler reads through each line of code
Then the assembler reads through the assembly language program again - 23Second read though– Now it generates the words of object code for each statement
– If there is a reference to a label that appears farther on (e.g. load x, or
jump loop) it looks up the value of the label in the symbol table - 24Program structure– Good comments explaining what it is
– The actual program (a sequence of instructions)
– An instruction to stop the program: trap R0,R0,R0
– Declarations of the variables: data statements - 25Why do we put the instructions first, and define the variables at the end?The computer will start executing at memory address 0, so there had
better be in instruction there, not data - 26Control registersThere are several more registers that the machine uses to keep track of what it's doing
They are (mostly) invisible to the program
PC register
IR
ADR - 27PC registerThe PC register (“program counter”) contains the address of the next instruction to be executed
- 28IRThe IR (“instruction register”) contains the instruction being executed right now
- 29ADRIf an RX instruction is being executed, the ADR (“address register”) contains the memory address of the second operand
- 30Fields separated by spaces– Label (optional)
– Operation
– Operands
– Comments - 31Good style• It isn't enough just to get the assembler to accept your program without error
messages
• Your program should be clear and easy to read
• This requires good style
• Good style saves time writing the program and getting it to work
• A sloppy program looks unprofessional - 32Comments• In Sigma16, a semicolon ; indicates that the rest of the line is a comment
• You can have a full line comment: just put ; at the beginning
• You should use good comments in all programs, regardless of language
• But they are even more important in machine language, because the code
needs more explanation
• At the beginning of the program, use comments to give the name of the
program and to say what it does - 33Use spaces, not tabs• To indent your code, always use spaces
• Don't use tabs!
• In general, never use tabs except in the (rare) cases they are actually required
– The tab character was introduced to try to mimic the tab key on old
mechanical typewriters
– But software does not handle tab consistently
– Using tabs, code can look good in one application and a mess in another - 34Address arithmeticpiece of data in the computer (in registers, or memory) is a word of bits
Addresses are unsigned numbers: 0, 1, 2, … , 65535 - 35Powerful control structures– Input/Output
– Procedures and functions
– Recursion
– Case dispatch
– Coroutines, classes, methods - 36ordinary variableholds one value (e.g. an integer)
- 37Data structuresA data structure is a container that can hold many individual elements
The key idea: we will do arithmetic on addresses - 38Arraysmathematics, an array (vector) is a sequence of indexed values x0, x1, ..., xn - 1
– x is the entire array
– x3 is one specific element of the array with index 3
– It's useful to refer to an arbitrary element by using an integer variable as
index (subscript notation): xi
– x is the entire array
– x3 is one specific element of the array with index 3
– It's useful to refer to an arbitrary element by using an integer variable as
index (subscript notation): xi - 39Arrays programming languagesIn programming languages, we refer to xi as x[i]
- 40Representing an arrayAn array is represented in a computer by placing the elements in consecutive memory locations
x is the address of its first element x[0]
The elements follow in consecutive locations - 41What about big arrays?In large scale software, arrays are allocated dynamically with help from the operating system
– It uses a trap to request (from the operating system) a block of memory big enough to hold the array
– The operating system returns the address of this block to the user program - 42Effective address– The displacement is a 16 bit constant: you can write the number, or use
a name (the assembler will put in the address for you)
– The index register is written in brackets
The machine adds the displacement to the value in the index register - 43Addressing modesis a scheme for specifying the address of data
Sigma16 has one addressing mode: displacement[index]
Many older computers provided many addressing modes but it's more efficient to provide just one or two flexible addressing modes, rather than a big collection of them - 44Array traversalA typical operation on an array is to traverse it
That means to perform a calculation on each element - 45For loops• A for loop is designed specifically for array traversal
• It handles the loop index automatically
• It sets the index to each array element index and executes the body - 46A useful conventionThe instruction set is designed to be regular, and follow consistent conventions
– This makes programming easier
– It also helps with the hardware design!
For most instructions, the operands follow the pattern of an assignment
statement: information goes right to left exception: store - 47Copying one register to anotheradd Rd,Ra,R0
– It's actually more efficient than providing a separate instruction just to
copy the register! - 48Using load and store– We need to keep variables in memory (most of the time) because
memory is big (there aren't enough registers to hold all your variables)
– The computer hardware can do arithmetic on data in registers, but it
cannot do arithmetic on data in memory - 49Therefore, to do arithmetic on variables, you must• Copy the variables from memory to registers (load)
• Do the arithmetic in the registers (add, sub, …)
• Copy the result from registers back to memory (store)
Información
- Descripción
- Computer Science Fichas sobre Week 5, creado por Isaac Gilbert el 08/02/2022.
- Creado por
- Isaac Gilbert
- Visitas
- 0
- Valoración