CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Final
CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Final
Name:
i
======================================== ---------------------------------------- ---------------------------------------- ----------------------------------------
CIS 2107 Summary Card 28-jan-08 Other Commands Storage Classes and Linkage of Objects Signed type conversions
======================================== (An object is a named region of storage)
hexdump -C file.c hex dump char -> short -> long-> float -> double
======================================== objdump -d prog.o disassemble .o 1. Internal (to a block or function) ----------------------------------------
Unix objdump -d prog disassemble exe a. automatic – known only in block Integer constants (suffix u, U, l, L)
======================================== objdump -x prog.o header contents after def. No init. Discard on 123 32-bit int
Compiling and executing a C program gdb prog debug prog exit. (int i; auto int i;). 123UL 32-bit unsigned long
x/20b main examine 20 bytes of main b. static – known only in block after 0100 100 octal is 64 decimal
unix> nano hello def. Init to zero. Preserved on 0x100 100 hexadecimal is 256 decimal
#include <stdio.h> For help with any of the following, use exit. (static int i;)
int main() "cmd --help" or "man cmd" or "info cmd" 2. External (to all blocks). Floating constants (suffix f, F, l, L)
{ as, cpp, gcc, gdb, help, info, a. external linkage (default). Known 12.3 64-bit double
printf("Hello world\n"); ld, man, objdump, od in file after def (1) or anywhere 12.3F 32-bit float
} ---------------------------------------- after extern (many). Init to zero. 12.3L 80-bit long double
^x (int i; extern int i;) 123e-01 == 12.3
unix> gcc hello.cn ======================================== b. internal linkage using static
unix> ./a.out C Programming Language keywork – known in file below def. Character and String Constants
Hello world ======================================== or in file after extern. Init to 'x' 8-bit character constant
unix> Identifiers zero. (static int i;) 'a' == '\101' == 'x41' == 61
---------------------------------------- ---------------------------------------- "string" array of 7 char ('\0' added)
unix>hexdump -C hello.c 1. Letters, numbers, underscores "_". Derived Types
00000000 23 69 6e 63 6c 75 64 65 20 3c 2. Begin with a letter (library routines Enumeration Constants
73 74 64 69 6f 2e |#include <stdio.| use names beginning with "_"). 1. arrays of objects of a given type enum day {sun=1, mon, tues, wed}
00000010 68 3e 0a 69 6e 74 20 6d 61 69 3. Keywords reserved. 2. functions returning objects of type ----------------------------------------
6e 28 29 0a 7b 0a |h>.int main().{.| 4. Declaration - interpretation of 3. pointers to objects of a given type C Operators in decreasing precedence
00000020 20 20 20 20 70 72 69 6e 74 66 identifier, many times. 4. structures containing a sequence of
28 22 48 65 6c 6c | printf("Hell| 5. Definition - declares and reserves objects of various types () (grp) [] (sub) l->r
00000030 6f 20 44 72 2e 20 42 6f 62 20 storage, only once. 5. unions containing any one of several struct -> (mbr) . (mbr) l->r
5c 6e 22 29 3b 0a |o Dr. Bob \n");.| ---------------------------------------- objects of various types unary ! (not) ~ (1's) ++ (inc) l->r
00000040 7d 0a |}.| keywords ---------------------------------------- unary -- (dec) + (pos) - (neg) r->l
unix> Declaration examples. unary * (ind) & (adr) r->l
---------------------------------------- auto double int struct unary (type) sizeof r->l
unix> gcc -E -o hello.i hello.c break else long switch int var; 32-bit signed int arith / (div) * (mul) % (mod) l->r
unix> gcc -S -o hello.s hello.i case enum register typedef int *var; pointer to int arith + (add) - (sub) l->r
unix> gcc -c -o hello.o hello.s char extern return union int var[3]; array of 3 ints shift << (lft) >> (rgt) l->r
unix> gcc -o hello hello.o const float short unsigned int *var[3]; array of 3 ptrs to ints relat < (ls) <= (le) > (gt) l->r
unix> ./hello continue for signed void int *(var[3]); array of 3 ptrs to ints relat >= (ge) == (eq) != (ne) l->r
Hello world default goto sizeof volatile int (*var)[3]; ptr to array of 3 ints bit & (and) ^ (exor) | (or) l->r
unix> do if static while int f(void); function returns int logic && (and) || (or) l->r
---------------------------------------- ---------------------------------------- int *f(void); function returns ptr if ? (true) : (false) r->l
type specifiers to int assign = += -= r->l
+---------+ +--------+ int (*p)(void); p is ptr to function assign *= r-l /= r->l
hello.c| pre- |hello.i| | char int unsigned union that returns int assign %= &= ^= r->l
------>|processor|------>|Compiler|-->| double long struct unsigned void f(int *a); arg to f is ptr to int assign |= <<= >>= r->l
| (cpp) | | (cc1) | | enum short typedef void void f(int *); same - "a" is a comment stmt , l->r
+---------+ +--------+ | float ----------------------------------------
v ---------------------------------------- int x[2][3][4]; /*definition*/ Operators
|<------------------------------------- Basic Data Types on x86 unix x[1][1][1] is int
| x[1][1] is array of 4 ints 1. struct.member, union.member
| +---------+ +------+ 1. char - 8-bit signed int, -128 to 127. x[1] is a 3x4 array of ints 2. ptr_to_struct->member
V hello.s| |hello.o| |hello ASCII character from 0 to 127. x is a 2x3x4 array if ints 3. ptr_to_union->member
-------->|Assembler|------>|linker|----> 2. short - 16-bit signed integer. x[1][1], x[1], and x are pointers 3. struct->member <==> (*struct).member
| (as) | | (ld) | 3. long - 32-bit signed integer. ---------------------------------------- 4. *&var <==> var
+---------+ +------+ 4. int - 32-bit signed integer. Type names - declare an object and omit 5. exp1<<exp2 <==> exp1*(2**exp2)
5. long long – 64-bit signed integer the object name. Used in casts, function if no overflow
---------------------------------------- 6. float - 32-bit floating point number. declarations, and sizeof 6. exp1>>exp2 <==> exp1/(2**exp2)
gcc commands 7. double - 64-bit floating point. if positive and no overflow
8. long double - 80-bit floating point. type name declaration 7. for negative exp1, exp1>>exp2 may
gcc -E -o prog.i prog.c preprocess only 9. void - nonexistant value fill with 0 (unsigned) or 1 (signed)
gcc -S -o prog.s prog.c plus compile ---------------------------------------- int int var; ----------------------------------------
gcc -c -o prog.o prog.c plus assemble Data Type Modifiers int * int *var; Typical Program Parts
gcc -o prog prog.c plus link int *[3] int *var[3];
gcc -v prog.c see detail 1. unsigned or signed (default) int (*)[] int (*var)[]; #include ...;
gcc -o prog one.c two.s three.o 2. const (never changed) int (int) int fun(int); #define ...;
---------------------------------------- 3. volatile (don't use optimization) int *(void) int *fun(void); function prototypes;
---------------------------------------- int (*[])(void) int (*var[])(void); external declarations and definitions;
---------------------------------------- int main(argc, *argv[] {...}
ret-type funct-name(arg-decl) {...}
ret-type funct-name(arg-decl) {...}
----------------------------------------
----------------------------------page 2 ---------------------------------------- ---------------------------------------- ========================================
Typical function parts (including main) typedef - new names for old data type Examples as Assembler (info as)
========================================
ret-type funct-name(arg-decl) typedef int Length; Length a, b, c; for (i=1, j=n; i>n; ++i, --j) {...} AT&T vs. Intel Syntax
{ void (*signal(int signum,
internal definitions and decl.; void (*func)(int)))(int); struct tnode *node = (struct tnode *) .intel_syntax noprefix
statement; typedef void Sigfun(int); malloc(sizeof(struct tnode)); .att_syntax prefix
statememt; Sigfun *signal(int signum, Sigfun *fun);
return expression; ---------------------------------------- for (p = head; p != NULL; p = p->next) AT&T Intel
} Structures and Unions
---------------------------------------- int factorial(int i) { push $4 push 4
Composition of Declarations & Statements struct OptStructName { return i<=1 ? 1 : i*factorial(i-1); clr %eax clr eax
declaration1; } jump *%eax jump eax
tokens: identifiers, keywords, constants declaration2; ---------------------------------------- addl $4, %eax add eax, 4
string literals, operators, other } OptVar1, OptVar2; ANSI C Standard Library movb movw mov (and examine
white space: sp, ht, vt, nl, ff, comment struct StructName Var3, Var4; ---------------------------------------- movl movq operands)
declaration: legal arrangement of tokens Input and Output <stdio.h> movb $10,achar mov byte ptr achar,10
statement: legal arrangement of tokens struct point { FILE *f, char *s, *t, int c, i, j
---------------------------------------- int x; AT&T addr. SEC:DISP(BASE,INDEX,SCALE)
Types of Statements int y; f=fopen(s, t) i=fclose(f) Intel addr. SEC:(BASE+INDEX*SCALE+DISP)
} xy, *pxy i=fprintf(f, s, ...) i=printf(s, ...) ----------------------------------------
labeled-stmt. label: stmt; xy.x = 5; xy.y=10; i=sprintf(s, t, ...) i=fscanf(f, s, ...) Assembler (as) Syntax
expression-stmt. expression; pxy = &xy; i=scanf(s, ...) i=sscanf(s, t, ...)
compound-statement. {stmt; stmt; stmt;} pxy->x = 20; pxy->y = 30; i=fgetc(f) t=fgets(s, i, f) 1. /* this is a "multiline" comment */
selection-stmt. if, switch i=fputc(c, f) i=fputs(s, f) 2. # this is a "line" comment
iteration-stmt. while, do, for typedef struct Flt { /*little endian*/ i=getc(f) i=getchar(void) 3. Statements end at '\n' or ';'
jump-stmt. goto, continue, unsigned int fract:23; t=gets(s) i=putc(j, f) 4. Symbols (A-Z), (a-z), (0-9), (_.$)
break, return, unsigned int exp:8; i=putchar(j) i=puts(s) 5. symbol: - define symbolic address
---------------------------------------- unsigned int sign:1; i=ungetc(j, f) 6. .symbol - assembly directive
while, for, do, if } flt; ---------------------------------------- 7. symbol – op code or address
union Tstflt { Character Class Tests <ctype.h> ----------------------------------------
while (exp) {...} flt also3; int i, char c Assembler Operands
for (exp; exp; exp) {...} float f;
do {...} while (exp); } tstflt; i=isalnum(c) i=isalpha(c) i=iscntrl(c) zero, one, and two operand instructions
if (expression) {...} else {...} flt a3 = {0x400000,128,0}; i=isdigit(c) i=isgraph(c) i=islower(c) setc # set the carry bit
exp ? nonzerostmt : zerostmt; testflt.also3=a3; i=isprint(c) i=isspace(c) i=isupper(c) incl wage # increment long wage
---------------------------------------- print(“%12.10f\n”, tstflt.f); ---------------------------------------- movb $5,x(%eax) # x[%eax] = 5;
switch ---------------------------------------- string functions (incomplete) <string.h>
Binary Trees char *s, *t, const char *cs, *ct operands
switch (intexp) { size_t n, int c, i 1000 – mem. addr. 1000 contains oper.
case int1: stmts; struct tnode { beta – addr. beta contains oper.
case int2: stmts; break; int data; s=strcpy(s,ct) s=strncpy(s,ct,n) $55 - immediate operand 55
default: stmts; struct tnode *left; s=strcat(s,ct) s=strncat(s,ct,n) %eax – 32-bit eax reg. contains oper.
} struct tnode *right; i=strcmp(cs,ct) i=strncmp(cs,ct,n) (%eax)- eax contains addr. of operand
---------------------------------------- } s=strchr(ct,c) s=strrchr(ct,c) beta(%eax,%ebx,4) - address
Jump statements and statement labels s=strstr(cs,ct) n=strlen(cs) beta + %eax + 4*ebx contains oper.
struct tnode *node = (struct tnode *) s=strerror(n) ----------------------------------------
if (exp) goto error; malloc(sizeof(struct tnode)); ---------------------------------------- section - block of addresses at 00000000
... node->data = 1; Mathematical Functions (math.h>
error: stmts; node->left = node->right = null; double x, y, z, int n as predefined sections text, data, bss
z=sin(x) z=cos(x) z=tan(x) .text contains code
continue; /*end this iter; start next*/ typedef struct tnode Treenode; z=asin(x) z=acos(x) z=atan(x) .data initialized data (int i=5;)
break; /*end iteration or switch */ typedef struct tnode *Treeptr; z=atan2(y,x) z=sinh(x) z=cosh(x) .bss uninitialized data (int j;)
return exp;/*return to stmt after call*/ Treeptr node = (Treeptr) z=tanh(x) z=exp(x) z=log(x) named sections with .section directive
---------------------------------------- malloc(sizeof(Treenode)); z=log10(x) z=pow(x,y) z=sqrt(x) .section .rodata
C Preprocessor ---------------------------------------- z=ciel(x) z=floor(x) z=fabx(x) .section .note.GNU-stack,"",@progbits
Linked Lists ---------------------------------------- other sections
#include "filename" /* local dir */ Utility Functions <stdlib.h> undefined value is 0, ld will define
#include <filename> /* sys defined */ struct llist { double atof(const char *s) .comment .ident text goes here
#define name replacement text struct llist *next; int atoi(const char *s) as address=(SECTION)+(OFFSET INTO SECT)
#define forever for (;;) {} int value; long atol(const char *s) .comm defines symbol in .bss section
#define max(A,B) ((A) > (B) ? (A) : (B)) } int rand(void) ----------------------------------------
max(i++, j++); /* bad idea*/ for (p = head; p != NULL; p = p->next) void stand(unsigned int seed) symbols
#if #ifdef #ifndef #elif #else #endif ---------------------------------------- void *calloc(size_t nobj, size_t size)
Printf format specifiers (after %) void *malloc(size-t size) symbol=expression #same as .set
#if !defined(ABC) void free(void *p) .set symbol expression #same as =
#define ABC i,d ints x hex e,f,g double void exit(int status) .Lsymbol is local symbol (ld never sees)
#else u uint c char p pointer int abs(int n) 1:, 2:, 3: are local labels, 1f, 2b 3f
#undef ABC o octal s string int labs(long n) "." is location counter. myadr: .long .
#endif ---------------------------------------- ---------------------------------------- ----------------------------------------
----------------------------------------
----------------------------------page 3 ---------------------------------------- ---------------------------------------- ----------------------------------------
symbol attributes (plus name) AMD and Pentium 4 64-bit Registers Stack Frame Convert (sign extend) instructions
value # usually 32 bits 8 64-bit regs: %rax %rbx %rcx %rdx +----------------+ cbtw %al to %ax
type includes section # .text, etc %rdi %rsi %rbp %rsp | ... | cwtl %ax to %eax
.type name,@function # for functions 8 64-bit exten: %r8 %r9 %r10 %r11 +----------------+ cltd %eax to %edx,%eax
.type name,@object # for data %r12 %r13 %r14 %r15 |callers storage | ----------------------------------------
.size name,exp # .size myint, 4 8 32-bit exten: %r8d %r9d %r10d %r11d +----------------+ Classic Two Operand (source+dest->dest)
.size main, .-main # length of main %r12d %r13d %r14d %r15d %ebp-12 -->|my arg 2 |<--+ (add, adc, and, xor, or, sbb, sub, cmp)
---------------------------------------- 8 16-bit exten: %r8w %r9w %r10w %r11w +----------------+ |
Reserving and defining storage %r12w %r13w %r14w %r15w %ebp-8 -->|my arg 1 | | addX %reg,%reg adcX %reg,%reg
8 8-bit exten: %r8b %r9b %r10b %r11b +----------------+ | addX $imm,%reg adcX $imm,%reg
.byte 74, 0B01001010 # 74 2 times %r12b %r13b %r14b %r15b %ebp-4 -->|return to caller| my addX mem,%reg adcX mem,%reg
.byte 0x4A, 0x4a, 'J # 74 3 times 8 64-bit SSE %xmm0 %xmm1 %xmm2 %xmm3 +----------------+ stack addX %reg,mem adcX %reg,mem
a: .byte 1 # char a = 1; %xmm4 %xmm5 %xmm6 %xmm7 my %ebp -->|callers %ebp | frame <--+ addX $imm,mem adcX $imm,mem
b: .value 2 # short b = 2; ---------------------------------------- +----------------+ | |
c: .long 4 # int c = 4; 24 General Registers %ebp+4 -->|my storage | | | subX %reg,%reg sbbX %reg,%reg
d: .long 4 # long d = 4; +----------------+ | b subX $imm,%reg sbbX $imm,%reg
e: .float 4.0 # float e = 4.0; bit 31 16 15 8 7 0 %ebp+8 -->|my storage |<--+-%esp o subX mem,%reg sbbX mem,%reg
f: .double 8.0 # double f = 8.0; +----------------+--------+--------+ +----------------+ o subX %reg,mem sbbX %reg,mem
g: .ascii "abc" # char g[3]={97,98,99} %eax| %ax->| %ah | %al | push arg2 |callees arg 2 |<--- %esp k subX $imm,mem sbbX $imm,mem
h: .asciz "abc" # char h[3]= “abc”; +----------------+--------+--------+ +----------------+ |
i: .=.+100 # char i[100]; %ebx| %bx->| %bh | %bl | push arg1 |callees arg 1 |<--- %esp | andX %reg,%reg orX %reg,%reg
j: .comm symbol,length,align #j in .bss +----------------+--------+--------+ +----------------+ | andX $imm,%reg orX $imm,%reg
---------------------------------------- %ecx| %cx->| %ch | %cl | call |return addr |<--- %esp-+ andX mem,%reg orX mem,%reg
More Assembler Directives +----------------+--------+--------+ +----------------+ andX %reg,mem orX %reg,mem
%edx| %dx->| %dh | %dl | push %ebp |my saved %ebp |<--- %esp andX $imm,mem orX $imm,mem
.align 4 # addr%4==0 +----------------+--------+--------+ +----------------+ %ebp
.align 4,,15 # addr%4==0, max 15 %edi| | %di | movl %esp, | | xorX %reg,%reg cmpX %reg,%reg
.align 4,0,15 # same, but fill with 0 +----------------+-----------------+ %ebp +----------------+ xorX $imm,%reg cmpX $imm,%reg
.p2align 4,,7 # addr%2^4==0, max 7 %esi| | %si | ---------------------------------------- xorX mem,%reg cmpX mem,%reg
.file file.c # may disappear +----------------+-----------------+ Prefixes xorX %reg,mem cmpX %reg,mem
.fill repeat, size, value %ebp| | %bp | xorX $imm,mem cmpX $imm,mem
.globl symbol # visible to ld +----------------+-----------------+ Section override prefixes
.ident "comment" # to comment section %esp| | %sp | `cs', `ds', `ss', `es', `fs', `gs' testX %reg,%reg testX $imm,%reg
---------------------------------------- +----------------+-----------------+ Operand/Address size prefixes ----------------------------------------
expressions ---------------------------------------- `data16', `addr16', data32, addr32 Classic 1 Operand (op dest->dest)
Flag Register (low order 16 bits) lock and wait prefixes
result is abs. number or offset into rep, repe, repne for string instructions incX %reg decX %reg
section (text, data, bss, absolute) Carry flag ---------------------------+ (%ecx times) incX mem decX mem
operators -, ~, *, /, %, <<, >>, Parity flag ----------------------+ | rex family of prefixes (extended regs)
|, &, ^, !, +, -, Aux carry flag ---------------+ | | ---------------------------------------- notX %reg negX %reg
==, <>, !=, <, >, >=, <=, Zero flag ----------------+ | | | Instruction Abbreviations notX mem negX mem
&&, || (true = -1, false = 0) sign flag --------------+ | | | | ----------------------------------------
---------------------------------------- trap flag ------------+ | | | | | X = b, w, l, or q Shift and Rotate Instructions
| | | | | | mem = label(%base, %index, 1|2|4|8) (rol, ror, rcl, rcr, shl=sal, shr, sar)
======================================== 15 v v v v v v %reg = one of 24 general registers If no $imm, shift amount in %cl
386-486-Pentium-Core 2-Athlon +-------------------------------+ XX = bw, bl, bq, wl, wq, lq
======================================== | | | | | | | | | | | | | | | | | $imm = immediate operand salX $imm,%reg sarX $imm,%reg
386-486-Pentium "General" Registers +-------------------------------+ ---------------------------------------- salX $imm,mem sarX $imm,mem
^ ^ ^ ^ ^ ^ 7 0 Data Movement (source -> dest)
8 32-bit regs: %eax, %ebx, %ecx, %edx, | | | | | | shlX $imm,%reg shrX $imm,%reg
%edi, %esi, %ebp, %esp | | | | | +-- interrupt enable movX %reg, %reg movX %reg, mem shlX $imm,mem shrX $imm,mem
8 16-bit regs: %ax, %bx, %cx, %dx, | | | | +------ Direction flag movX mem, %reg movX $imm, mem
%di, %si, %bp, %sp | | | +--------- Overflow flag movX $imm, %reg rolX $imm,%reg rorX $imm,%reg
8 8-bit regs: %ah, %al, %bh, %bl, | +-+-------------- priv level rolX $imm,mem rorX $imm,mem
%ch, %cl, %dh, %dl +----------------- nested task movsXX %reg, %reg movsXX mem, %reg
1 32-bit eflags: movzXX %reg, %reg movzXX mem, %reg rclX $imm,%reg rcrX $imm,%reg
1 32-bit eip: ---------------------------------------- rclX $imm,mem rcrX $imm,mem
---------------------------------------- Calling Conventions pushl mem popl mem ----------------------------------------
Additional 386-486-Pentium Registers pushl %reg popl %reg Unsigned Multiply and Divide
caller saves %eax, %ecx, %edx if wanted pushl imm oper = %reg or mem
6 16-bit segment: %cs, %ds, %ss, %es, callee saves %ebx, %esi, %edi if used for divide, result is remainer,quotient
%fs, %gs exch %reg,%reg exch mem,%reg
4 32-bit ctrl: %cr0, %cr1, %cr2, %cr3 fn: pushl %ebp #save callers %ebp mulb oper %ax = %al * oper
6 32-bit debug: %db0, %db1, %db2, %db3, movl %esp,%ebp #create my %ebp leal mem,%reg mulw oper %dx,%ax = %ax * oper
%db6, %db7 ... ---------------------------------------- mull oper %edx,%eax = %eax * oper
2 32-bit test: %tr6, %tr7 movl %ebp,%esp #restore %esp divb oper %ah,%al = %ax / oper
8 80-bit flt pt: %st=%st(0), %st(1), popl %ebp #rest. callers %ebp divw oper %dx,%ax = %dx,%ax / oper
%st(2), ..., %st(7) ret divl oper %edx,%eax = %edx,%eax / oper
---------------------------------------- ---------------------------------------- ----------------------------------------
----------------------------------page 4 ---------------------------------------- ---------------------------------------- ----------------------------------------
Signed Multiply and Divide Floating Point (8 80-bit registers) ASCII Decimal-Binary-Hexadecimal (Hex)
oper = %reg or mem
for divide, result is remainder,quotient float a, b, c; double d, e, f; 10 16 ch 10 16 ch 10 16 ch 10 16 ch 10 2 16 10 2 16
a = b + c; d = e + f;
imulb oper %ax = %al * oper 0 00 \0 32 20 SP 64 40 @ 96 60 ` 0 0000 0 8 1000 8
imulw oper %dx,%ax = %ax * oper flds b #s=short fldl e #l = long 1 01 SOH 33 21 ! 65 41 A 97 61 a 1 0001 1 9 1001 9
imull oper %edx,%eax = %eax * oper flds c #p=pop fldl f #p = pop 2 02 STX 34 22 " 66 42 B 98 62 b 2 0010 2 10 1010 A
faddp %st,%st(1) faddp %st,%st(1) 3 03 ETX 35 23 # 67 43 C 99 63 c 3 0011 3 11 1011 B
imulX %reg,%reg fstps a fstpl d 4 04 EOT 36 24 $ 68 44 D 100 64 d 4 0100 4 12 1100 C
imulX &mem,%reg 5 05 ENQ 37 25 % 69 45 E 101 65 e 5 0101 5 13 1101 D
imulX $imm,%reg Floating Point Condition Codes. After 6 06 ACK 38 26 & 70 46 F 102 66 f 6 0110 6 14 1110 E
“fnstsw %ax; andb $69,%ah”, %ah = ($0, 7 07 BEL 39 27 ’ 71 47 G 103 67 g 7 0111 7 15 1111 F
idivb oper %ah,%al = %ax / oper $1, $64 or $69) for (>, <, =, or NAN) 8 08 BS 40 28 ( 72 48 H 104 68 h ----------------------------------------
idivw oper %dx,%ax = %dx,%ax / oper ---------------------------------------- 9 09 HT 41 29 ) 73 49 I 105 69 i Floating Point Numbers
idivl oper %edx,%eax = %edx,%eax / oper String Instructions 10 0A \n 42 2A * 74 4A J 106 6A j
---------------------------------------- 11 0B \v 43 2B + 75 4B K 107 6B k len sign exp hide frac
Jump, Call, and Ret Instructions %esi points to source, %edi to dest. 12 0C \f 44 2C , 76 4C L 108 6C l
%esi and %edi incremented by 1, 2, or 4 13 0D \r 45 2D - 77 4D M 109 6D m float 32 1 8 1 23
jmp label #uses %eip relative addr. if DF (direction flag) == 0 (cld) 14 0E SO 46 2E . 78 4E N 110 6E n
jmp *%reg #jump to addr in reg decrement if DF == 1 (std) 15 0F SI 47 2F / 79 4F O 111 6F o double 64 1 11 1 52
jmp *mem #jump to addr in mem loc movs move string, movX (%esi),(%edi) 16 10 DLE 48 30 0 80 50 P 112 70 p
cmps compare string, cmpX (%esi),(%edi) 17 11 DC1 49 31 1 81 51 Q 113 71 q long double 80 1 15 - 64
call pushes %eip (return addr) on stack stos store string, movX %a?,(%edi) 18 12 DC2 50 32 2 82 52 R 114 72 r
call label #uses %eip relative addr. lods load string, movX (%edi),%a? 19 13 DC3 51 33 3 83 53 S 115 73 s exponent bias is 127, 1023, or 16383
call *%reg #function addr in reg scas scan string, cmpX (%edi),%a? 20 14 DC4 52 34 4 84 54 T 116 74 t
call *mem #function addr in mem loc 21 15 NAK 53 35 5 85 55 U 117 75 u 31 30 23 22 0
ret #pop stack into %eip put count in %ecx and repeat instruction 22 16 SYN 54 36 6 86 56 V 118 76 v +-+--------+---------------------+
---------------------------------------- using rep, repe, and repne prefix 23 17 ETB 55 37 7 87 57 W 119 77 w float |s| exp |1. + 23-bit fraction |
Condition Codes ---------------------------------------- 24 18 CAN 56 38 8 88 58 X 120 78 x +-+--------+---------------------+
Read Time Stamp Counter 25 19 EM 57 39 9 89 59 Y 121 79 y
CF: Carry flag (unsigned overflow if 1) rdtsc - cycles since boot in %edx, %ecx 26 1A SUB 58 3A : 90 5A Z 122 7A z ----------------------------------------
ZF: Zero flag, set to 1 if result is 0 ---------------------------------------- 27 1B ESC 59 3B ; 91 5B [ 123 7B { Recognizing Floating Point Numbers
SF: Sign flag, equals result high bit ======================================== 28 1C FS 60 3C < 92 5C \ 124 7C |
OF: Overflow flag (unsigned overflow) Counting and Number Representation 29 1D GS 61 3D = 93 5D ] 125 7D } if e==0:
---------------------------------------- ======================================== 30 1E RS 62 3E > 94 5E ^ 126 7E ~ denormalized
Conditional Jump instructions, jCC label Signed and unsigned 8-bit bytes 31 1F US 63 3F ? 95 5F _ 127 7F DE f=(-1)^S * 2^(1-bias)*0.M
Use after cmp, add, sub, etc. (not mov) ---------------------------------------- else if e == all 1s:
cmpl a,b; jg label; jumps if b-a>0 (b>a) hex binary unsigned 2’s comp Escape sequences in ASCII if M == 0:
if S == 0:
Simple Condition Code Tests 00 00000000 0 0 null or zero NUL \0 = \000 = \x00 = 0 +infinity
01 00000001 1 1 audible alert BEL \a = \007 = \x07 = 7 else:
jo label jno label 02 00000010 2 2 backspace BS \b = \010 = \x08 = 8 -infinity
jz label jnz label 03 00000011 3 3 horizontal tab HT \t = \011 = \x09 = 9 else:
js label jns label ... ... ... ... newline (LF) NL \n = \012 = \x0a = 10 NaN
jo label jn0 label 7E 01111110 126 126 vertical tab VT \v = \013 = \x0b = 11 else:
7F 01111111 127 127 formfeed FF \f = \014 = \x0c = 12 normalized
signed unsigned 80 10000000 128 -128 carriage ret CR \r = \015 = \x0d = 13 (-1)^S * 2^(e-bias)*1.M
81 10000001 129 -127 double quote " \" = \042 = \x22 = 34
jg (jnle) label ja (jnbe) label ... ... ... ... single quote ' \' = \047 = \x27 = 39 ----------------------------------------
jge (jnl) label jae (jnb) label FE 11111110 254 -2 question mark ? \? = \077 = \x3f = 63 #define bit_get(p,m) ((p) & (m))
jl (jnge) label jb (jnae) label FF 11111111 255 -1 backslash \ \\ = \134 = \x5c = 92 #define bit_set(p,m) ((p) |= (m))
jle (jng) label jbe (jna) label ---------------------------------------- octal number ooo \ooo #define bit_clr(p,m) ((p) &= ~(m))
---------------------------------------- Signed and unsigned 32-bit integers hex number hh \xhh #define bit_flip(p,m) ((p) ^= (m))
Set Instructions (setCC reg8) ---------------------------------------- #define bit_write(c,p,m)
setCC reg8; movzbl reg8,reg32; hex unsigned 2’s comp Powers of 2 and 16 (c ? bit_set(p,m) : bit_clear(p,m))
#define BIT(x) (0x01 << (x))
Set Individual Condition Code Bits 00000000 0 0 n 2^n 16^n #define LONGBIT(x)
00000001 1 1 ((unsigned long)0x00000001 << (x))
seto label setno label 00000002 2 2 0 1 1 ----------------------------------------
setz label setnz label 00000003 3 3 1 2 16 struct one32bitlong {
sets label setns label ... ... ... 2 4 256 unsigned bit:1;
seto label setno label 7FFFFFFE 2,147,483,646 2,147,483,646 3 8 4,096 unsigned nibble:4;
7FFFFFFF 2,147,483,647 2,147,483,647 4 16 65,536 unsigned dozen:12;
signed unsigned 80000000 2,147,483,648 -2,147,483,648 5 32 1,048,576 unsigned fifteen:15;
80000001 2,147,483,649 -2,147,483,647 6 64 16,777,216 };
setg (setnle) reg8 seta (setnbe) reg8 ... ... ... 7 128 268,435,456 ----------------------------------------
setge (setnl) reg8 setae (setnb) reg8 FFFFFFFD 4,294,967,294 -3 8 256 4,294,967,296
setl (setnge) reg8 setb (setnae) reg8 FFFFFFFE 4,294,967,294 -2 9 512 68,719,476,736
setle (setng) reg8 setbe (setna) reg8 FFFFFFFF 4,294,967,295 -1 10 1,024 1,099,511,627,776
---------------------------------------- ---------------------------------------- ----------------------------------------
CIS 2107 Final
Computer Systems and Low-Level Programming December 15, 2011
(1 point) (a) The auto storage class (used for local variables in a function) uses this area of memory.
(a)
(1 point) (b) Translates a high level language program into assembly language.
(b)
(1 point) (c) Data structure created by the assembler during pass 1.
(c)
(1 point) (d) Roughly one million times slower, cheaper, and larger than main memory or cache.
(d)
(1 point) (e) Technology used for main memory.
(e)
(1 point) (f) Tracks are partitioned into these (typically 512 bytes each).
(f)
(1 point) (g) Malloc and free manage this area of memory.
(g)
(1 point) (h) Stores each bit in a cell composed of six transistors.
(h)
(1 point) (i) One of the concentric rings on the surface of a disk.
(i)
(1 point) (j) Technology used to create solid state disks.
(j)
1 of 13 question 1 continues . . .
CIS 2107 Final
Computer Systems and Low-Level Programming December 15, 2011
(The answers are repeated at the top of this page for your convenience. They are the same as the answers
on the previous page.)
(k)
(1 point) (l) Tendency for programs to access multiple objects in a block.
(l)
(1 point) (m) Translates assembly language programs into machine language.
(m)
(1 point) (n) Area of memory used for initialized global variables.
(n)
(1 point) (o) In a collection of disk platters, a set of tracks equidistant from the center of the platter.
(o)
(1 point) (p) Processor register that contains the address of the next instruction to be executed.
(p)
(1 point) (q) Contains the return value of functions which return ints.
(q)
(1 point) (r) The larger but slower cache. Still much faster than main memory.
(r)
(1 point) (s) Time it takes to read from disk.
(s)
(1 point) (t) Time to read from on-CPU cache.
(t)
1 1 1 1 0 1 0 1 1 0 1 02
+ 1 0 1 1 1 1 1 0 1 12
5 6 9 6 C 1 B16
+ D A 7 7 816
(5 points) 4. The hex value 0x411a0000 is stored in a 32-bit C float variable. What floating point number does this
value represent? It’s perfectly OK if your answer includes fractions. (For example, if the correct answer
were 2.75, you could also write “2 and 43 ”.)
(10 points) 5. Some bit operations. If we have char x = 0x5C, y = 0xA9;, what is the result of the following
operations? Your answer must be in the form of exactly two hex digits1 .
(a) x|y
(a)
(b) x||y
(b)
(c) x<<2
(c)
(d) ˜x
(d)
(e) ˜˜x
(e)
(f) x&0x0F
(f)
(g) x∧ y
(g)
1 Ignore the possibility of promotion to 32-bit ints. Behave as though we’re living in the land of 8-bit arithmetic.
(h) x&&1
(h)
(i) -x
(i)
(j) x-y
(j)
(k) !!x
(k)
(l) x<<1
(l)
(m) x&y
(m)
(n) x∧ y∧ y
(n)
(o) x|0
(o)
CIS 2107 Final
Computer Systems and Low-Level Programming December 15, 2011
int *p=&a;
int *q=p;
char *cp = (char*)q;
(*p)--;
q--;
cp--;
and memory is laid out like this:
cp 1000
q 1004
p 1008
b 1012
a 1016
(a)
(b) &a
(b)
(c) b
(c)
(d) p
(d)
(e) *p
(e)
(f) &p
(f)
(g) q
(g)
(h) *q
(h)
(i) cp
(i)
(j) &cp
(j)
(10 points) 7. Use the following code to answer the questions. Data sizes are specified on the cover of the exam.
1 struct Stuff { 26
2 int x; 27 return 0;
3 int *p; 28 }
4 int A[10]; 29
What is the value of each of the following after func04( ) has been called?
(d) x
(d)
(e) y
(e)
(f) A[0]
(f)
(g) s.x
(g)
(h) s.A[0]
(h)
(i) *(s.p)
(i)
(j) str (What’s the string?)
(j)
(10 points) 8. Write a C function which is passed an unsigned int x. The function returns 1 if there are an odd number
of 1s in x’s binary representation or 0 otherwise.
...
return x+y-t;
}
Immediately before func( ) is called, i.e., immediately before the instruction call func, %ebp contains
the value 100010 and %esp contains the value 96010 . Before func( ) exits (more precisely, just before
the leave and return instuctions are executed), what is:
(1 point) (a) stored in %ebp?
(a)
(1 point) (b) stored in %esp?
(b)
(1 point) (c) the location of x?
(c)
(1 point) (d) the location of y?
(d)
(1 point) (e) the most likely location of t?
(e)
(1 point) (f) the location of the return value?
(f)
(10 points) 10. Write a C function equivalent to the following assembly (no credit for an answer containing inline
assembly).
1 .section .text
2 .globl mystery
3 .type mystery, @function
4 mystery:
5 pushl %ebp
6 movl %esp, %ebp
7 xorl %eax, %eax
8 xorl %ecx, %ecx
9 movl 8(%ebp), %edx
10 begin:
11 cmpl 12(%ebp), %ecx
12 jge done
13 addl (%edx, %ecx, 4), %eax
14 incl %ecx
15 jmp begin
16 done:
17 movl %ebp, %esp
18 popl %ebp
19 ret
(10 points) 11. Implement the function void reverse(int A[ ], int len), which reverses the order of A[ ], an array
of len items. Do not use the [ ] operator. No credit will be given for solutions which use the [ ]
operator, or which declare len or more elements of temporary storage.
(10 points) 12. A common way of storing a spreadsheet is comma-separated text. For example, the following line in a
spreadsheet:
could be stored as “apple, banana, cherry, some fruit beginning with d”. Write the function char **split(char *s)
which is passed s, which is a string of comma-separated values, and returns an array of string containing
the values in the line terminated by a NULL pointer. Using our current example, we’d return:
w[0] apple
w[1] banana
w[2] cherry
w[3] some fruit beginning with d
w[4] NULL
split( ) should return NULL on failure. Hint: if there are n commas in s, there will be n + 1 words.
You may use any function in the Standard C Library.
(extra space)
13 of 13 end of exam