Tom Kelliher, CS18
Feb. 16, 1996
C/C++ less abstract than BASIC
Plus unsigned
char string[] = "Hello, world.\n";
int data[10] = { 1, 2, 3, 4 };
cout << string;
string[5] = '\0';
cout << string << endl;
data[4] = 29;
struct employeeRecord
{
char name[NAMELEN];
int age;
int salary; // unit is cents
};
employeeRecord tom;
strcpy(tom.name, "Thomas P. Kelliher";
tom.age = 34;
tom.salary = 500;
Main memory --- RAM
Running program allocates memory for different purposes:
Each instruction stored in a word of memory
Consider the C code:
taxes = 1000; netPay = grossPay - taxes;
The corresponding assembler code:
li $16, 1000
sw $16, taxes($0)
lw $17, grossPay($0)
sub $18, $17, $16
sw $18, netPay($0)
C code:
if (num > max) max = num;
Assembler code:
lw $16, num($0)
lw $17, max($0)
ble $16, $17, eif
sw $16, max($0)
eif: ...
C code:
while (i < 10)
{
sum += i;
i++;
}
Assembler code:
lw $16, i($0)
lw $17, sum($0)
while: bge $16, 10, ewhile
add $17, $17, $16
add $16, $16, 1
j while
ewhile: ...
All values are binary
Signed, unsigned two's complement representation
Consider the four bit number:
Signed value:
Unsigned value:
Examples:
Smallest, largest values for each representation?
Formulas for ranges of n-bit numbers?
Examples
Locating a specific element: indexing
Address of the element array[i]:
address = AddressOfStartOfArray + sizeof(elementType) * i
Examples