Computer Systems Org I - Prof. Grishman

Lecture 23 - Nov. 22, 2005

Exam review

Note on arrays in C and Java:
    In C, arrays are allocated when they are declared, either as static arrays or on the heap.
    Original C only allowed constant array dimensions, so the offset of each array in a stack frame would be fixed.
    gcc permits variable dimensions, which are more complex to implement.
    In contrast, Java arrays are allocated by a new operation, and become objects in the heap.

Notes on assignment #6 (interpreter project)

Dereferencing invalid pointers can lead to segmentation errors, which can be hard to track down.  The Gnu debugger, gdb, can help.  Download it as part of Cygwin (in the development section of programs).  Then to run gdb,
    gcc -g myprogram.c  (compiles program with debug information for gdb)
    gdb a.exe
    (gdb) run  (runs the program until it crashes or ends)
    (gdb) where  (gives a traceback)
    (gdb) print var   (prints any local variable)
    (gdb) quit
More information on gdb is available on-line... 

http://www.cs.princeton.edu/~benjasik/gdb/gdbtut.html is an excellent introduction to gdb 
http://www.delorie.com/gnu/docs/gdb/gdb_toc.html is more comprehensive.

x86 Architecture, cont'd

Byte addressability

On the LC-3, the basic unit of memory was the (16-bit) word.  This led to some inefficiencies for strings ... either we stored a string one character per word (an inefficient use of memory) or two characters per word (much more complex, and slower, code).

The x86 is byte addressable.  The unit of memory is the (8-bit) byte, and individual bytes can be loaded, compared, and stored.  Other instructions manipulate pairs of bytes (a 'word') as a unit, or sets of 4 bytes as a unit (a 'double word').  This makes the machine more complicated but more flexible.

One can set aside space for a single byte

QUACK  DB   5

for a string of bytes (which you have to zero-terminate yourself)

QUACK  DB  "Hello.", 0

or for a word

QUACK  DW   1776

Bit manipulation

One area in which the LC-3 is particularly weak is bit manipulation.  Sometimes we need to get the i-th bit of a word;  this is not easy on the LC-3.  Getting the i-th through j-th bits is even harder.

The x86 provides a rich set of operations for bit manipulation.  First, it provides all the logical operations,
NOT
AND
OR
XOR
Then it provides a rich set of shift operations
SHL - shift left
SHR  - shift right (logical)
SAR  - shift right (arithmetic)
ROL  - rotate left
ROR  - rotate right
RCL  - rotate through carry left
RCR  - rotate through carry right
The shift operations take two operands, the quantity being shifted (a register or memory) and a shift count.  The 8086 had the constraint that the shift count must be either 1 or the contents of register CL.  We will live with this constraint because we are using Dewar's simulator.  Later x86 processors allowed a more general shift count.