Sequential Prog. Lec 1
Sequential Prog. Lec 1
Sequential Prog. Lec 1
PROGRAMMING
0111(7)
+0011(3)
1010
Programs can be written in machine language, assembly, higher-level
languages... whatever. Since each is an abstraction of a lower level to
which we can build a compiler, all of them will give us the same final
result.
A computer sees a string such as "10000011" as a sequence of bits,
without regards to any sort of encoding. This could bebinary ,
hexadecimal, decimal ASCII
, ... pretty much everything, but the computer
can not know which.
Negative Numbers With n bits, can be represented 2n numbers in binary.
To represent negative numbers we can use the sign and magnitude sign
and magnitude: use the first bit as an indicator of sign and the remaining
bits as magnitude. method, though this is subject to some weirdness. We
will end up with both a positive and a negative zero and will need
specialized hardware for both addition and subtraction.
The Decimal Positional Numbering System
Ten figures: 0 1 2 3 4 5 6 7 8 9
7 x 102 + 3 x 101 + 0 x 100 = 7301
0
00111010
3A
American Standard Code for Information Interchange (ASCII)
ASCII was invented to communicate text. ASCII can represent characters such as A-Z,
a-z, 0-9, and control characters like ();!. Since ASCII uses 7 bits, 27 = 128 characters can
be represented with ASCII. As a consequence of that, ASCII is basically only for Roman,
unaccented characters, although many people have created their own variations of ASCII
with different characters
ASCII is a set of meanings for 7-bit sequences.
Bits ASCII Interpretation
0001010 LF (line feed)
1000111 G
1100111 g
0111000 8
In the last case, 0111000 represents the character `8', not the unsigned big- or little-endian
number 8.
Extended ASCII
1. 0 is always equal to 0
Here is one example for jr31.hex :
; 0000 0011 1110 0000 0000 0000 1000
;03e0008
.word 0x03e0008
Conti..
LIS is another useful command; the LIS command will run
$d = MEM[PC] PC = PC + 4
Which can be used to assign a register to any value of your choice.
Assembly Language
There is a one-to-one correspondence between each assembly and machine instruction. The
assembly can be converted directly into machine language with an assembler.
An assembler will read in a file and scan through it, recognizing each line to be a discrete
instruction. It will remove comments and divide the instructions into words and tokens. This
first step is referred to as scanning
scanning: taking the sequence of characters and outputting a sequence of tokens
This is also referred to as lexical analysis or tokenization.
There are multiple types of tokens, for example: opcode (ID), register (REG), comma. These
tokens will also be associated with what we refer to as a lexene
lexene: the string which was recognized as a given token
For example, the lexene of a REG token may be $29.
Loading
A loader is a program that places a file into RAM. For N instructions, we do:
For (int i = 0; i < N; i++) {
MEM[i] = file[i];
}
Note that we can’t necessarily assume that we begin at memory address zero. That is
where MERL (MIPS Executable Relocatable Locatable code) comes into play.
We can add additional information to our file at either the beginning (beq $0, $0, n; ...;
begin:) or the end (jr $31; ...). Often we use both: the beginning of our code contains the
address of the relocation table at the end of our code.
In effect, a relocator will read in the first n bytes of code, follow those to memory
addresses, and add α to them (after the code has already been compiled by assuming
an initial pc of zero).
For example, if a program beginning with example: .word example is meant to be run
beginning at memory address 0x8, this first line would be first compiled as 0000 0000
then relocated as 0000 1000.
Relocator
A relocator uses the following algorithm (note that the relocation table follows the sample format .word
1; .word x; .word 1; .word y...
i = alpha + MEM[alpha + 8]
end = alpha + MEM[alpha + 4]
while i < end:
if MEM[i] == 1:
MEM[alpha + MEM[i + 4]] = MEM[alpha + MEM[i + 4]] + alpha i += or,
in words
1. i is the address of the start of the relocation table
2. end is the address of the end of the relocation table
3. check if MEM[i] has value 1
4. get the original address of a location to be relocated (MEM[i + 4]) and overwrite it with the location
after the offset (MEM[i + 4] + alpha)
Advantages of Assembly Language
An understanding of assembly language provides knowledge of:
Interface of programs with OS, processor and BIOS;
Representation of data in memory and other external devices;
How processor accesses and executes instruction;
How instructions access and process data;
How a program accesses external devices.
Other advantages of using assembly language are:
It requires less memory and execution time;
It allows hardware-specific complex jobs in an easier way;
It is suitable for time-critical jobs;
It is most suitable for writing interrupt service routines and other memory resident programs.
Addressing Data in Memory