Fichas
Week 8
Fichas del conjunto (26)
- 1“Short circuit” expressionsi0
• If the first expression i0
• We can “short circuit” the evaluation
– Minor advantage: less time to evaluate the expression
– Big advantage: if i - 2Condition codeThe cmp instruction sets a result (less than, equal, etc) in R15 which is
called the condition code, there are conditional jumps for all the results
• An advantage is that you don't need to use a register for the Boolean result - 3Repeat-until loop• Similar to a while loop, but the decision to continue is at the end of the loop
repeat
{S1; S2; S3}
until i>n;
• This is equivalent to
S1; S2; S3;
while not (i>n) do
{S1; S2; S3}
• The while loop is used far more often, but if you need to go through the loop
at least one time, the repeat-until is useful - 4Input/Output• Sigma16 uses UCS-2, a form of Unicode with 16 bits per character
– ASCII is a subset of UCS-2
• Lower case a…z have codes (in decimal) 97...122
• Upper case A...Z have codes (in decimal) 65...90
• Digit characters 0...9 have codes (in decimal) 48...57 - 5Converting a number to a string• We need to divide the number by 10 to get the quotient and the remainder
• div R1,R2,R3
– Divides R2/R3
– The quotient goes into R1 (the destination register)
– The remainder goes into R15 (always R15, you cannot change this)
• The algorithm repeatedly divides the number by 10
– The remainder is used to get a digit character - 6We have seen how to access an array element using an index register; R5 := x[i]
load R2,i[R0] ; R2 := i
load R5,x[R2] ; R5 := x[i] - 7Sum of an array x using index: high level• Suppose x is an array of numbers, and sizeX is the number of elements
– We want to add up all the elements of x
• A for loop is convenient (the purpose of for loops is writing this kind of loop)
sum := 0;
for i := 0 to sizeX-1 do
{ sum := sum + x[i]; }
• You can also use a while loop
sum := 0;
i := 0;
while i < sizeX do
{ sum := sum + x[i];
i := i + 1; } - 8Accessing an array element using a pointer• Create a pointer p to the beginning of the array x, so p is pointing to x[0]
lea R1,x[R0]
• To access the current element, follow p
load R2,0[R1]
• To move on to the next element of the array, increment p
lea R1,1[R1] ; we are doing arithmetic on pointers - 9Sum of an array x using pointers: high level• In assembly language, we use lea to increment the pointer
• Suppose p is in R1, then lea R1,1[R1] ; p := p + 1
• We are incrementing p by the size of an array element, i.e. by 1 - 10Accessing elements of an array using index– Get x[i] with load R5,x[R1] where R1=I (R1 is the index)
– Move to next element of array by i := i+1
– Determine end of loop with i < xSize
– Know in advance how many iterations: xSize (elements of the array)
– A for loop is convenient - 11Accessing elements of an array using pointers– Initialize p with lea R1,x[R0]
– Get x[i] with load R5,0[R1] where R1=p (R1 is the pointer)
– Move to next element of array by p := p+1
– Determine end of loop with p < q (q points to end of array)
– Don't need to know in advance how many iterations (elements), we know xEnd
– Need to use a while loop - 12Stack overflow• The mechanism for calling a procedure and returning is fairly complicated
• Rather than introducing all the details at once, we have looked at several
versions, introducing the concepts one at a time
• Now we introduce the next level
– Simplify calling a procedure
– The procedure checks for stack overflow - 13R12holds stack top (highest address in current stack frame)
- 14R11holds stack limit (stack not allowed to grow beyond this address)
- 15Stack overflow • If the stack is full and a procedure is called, this is a fatal errorStackOverflow
lea R1,2[R0] ; code for write
lea R2,StackOverflowMessage[R0] ; address where message starts
lea R3,15[R0] ; string length
trap R1,R2,R3 ; print "Stack overflow\n"
trap R0,R0,R0 ; halt
StackOverflowMessage
data 83 ; 'S'
data 116 ; 't'
data 97 ; 'a'
data 99 ; 'c'
data 107 ; 'k - 16block• One important concept is a block (or compound statement)
– A single statement that contains several statements
– The purpose is to let you have a group of statements in a loop, or
controlled by a conditional - 17syntax style for blocks\ Python Algol/Pascal CThe layout (the indentation) determines what is inside the if statement. In Python, you write : to mean then, and else to mean else.
Indentantion is ignored by the compiler. Statements must be separated by semicolon ; You write then to mean then, and else to mean else.
– The condition x < y must be enclosed in parentheses so the compiler
can tell where the condition ends and the then-statement begins. The semicolon ; is part of the statement - 18Block structured style for blocks: matching keywordsThis style introduces a lot of keywords (endif, endwhile, endfor, endrepeat)
but you don't need braces around a block
– It makes code more readable and enables the compiler to produce better
error messages (easier for the compiler) - 19Enter at beginning, exit at end• A common programming style is to require
– Each block enters only at the beginning of the block
– Each block exits only at the end of the block
• This style is helpful in some programming languages, but in some languages
it sometimes makes code less readable
• In assembly language, and for compilation patterns, it is necessary to follow
this style
• In high level languages this style is sometimes helpful, but not always - 20Single entrance/exit for compilation patterns• It is straightforward to translate high level control constructs using the
compilation patterns
– These require that the blocks of code always start at the beginning and
finish at the end
• It's bad to jump into the middle of a block, or exit out of the middle because
– The compilation patterns won't work correctly
– You'll have to duplicate a lot of code - 21Why use this systematic approach to programming?• Enables to write correct code from the beginning and minimise debugging
• If there is a bug, it helps to catch it early (e.g. in translation to goto form)
• If there's a bug in an instruction, the comments enable you to find it quickly
• Professional software needs to be maintained - 22The case statement• This means, execute the statement corresponding to the value of n
• Many programming languages have this, but the syntax varies a lot - 23Finding a numeric code• It's tedious and inefficient to go through the possible values of a numeric
code in sequence
Solution: a technique called jump tables - 24Jump tables: the basic idea• For each target statement (S1, S2, S3, etc) in the conditional, we introduce a
jump to it
– jump S1[R0], jump S2[R0], etc
• We make an array “jt” of these jump instructions
jt[0] = jump S0[R0]
jt[1] = jump S1[R0]
jt[2] = jump S2[R0]
...
• Given the code, jump to jt[code]
• Each element of the array is an instruction (RX) that requires two words
– So jump to &jt + 2 x code - 25A real world incident! MAC• On 21 February 2014, Apple announced a bug in core security software
– It affected iPhone, iPad, Macintosh, Safari, etc
“padlock icon” on a browser, it means the SSL protocol is
being used (also, https instead of http)
• The second goto fail is unconditional so the check in the last if statement is
never executed - 26A real world incident! LinuxA mystery: The Attempted Linux Trapdoor Hack of 2003
The master copy of Linux was maintained on Bitkeeper
– No longer, now git is used
• A secondary copy was on cvs
if ((options == (__WCLONE | __WALL)) && (current->uid = 0))
retval = -EINVAL;
]
• In C, == is the comparison operator and = is the assignment operator (bad
notation, but we're stuck with it)
• This code looks like it is checking
– If either the WCLONE or WALL option flag is set and the current user is
root, then return an error condition
• What it actually says is
– Check the irrelevant flags, set the current user to be root, get the
irrelevant result 0 and do nothing else
Información
- Descripción
- Computer Science Fichas sobre Week 8, creado por Isaac Gilbert el 01/03/2022.
- Creado por
- Isaac Gilbert
- Visitas
- 0
- Valoración