Cse 322 Programming Languages and Compilers Lecture 16

  • Slides: 23
Download presentation
Cse 322, Programming Languages and Compilers Lecture #16, May 24, 2007 • Runtime. c

Cse 322, Programming Languages and Compilers Lecture #16, May 24, 2007 • Runtime. c • Running the code • debugging assembler • division • strings for println • making test files 6/16/2021 1

Cse 322, Programming Languages and Compilers Project #3 • project #3 is due Thursday,

Cse 322, Programming Languages and Compilers Project #3 • project #3 is due Thursday, June 7, 2007 at 5: 00 PM – this is in 14 days. – In order to get the course graded, there will be no extensions • Final exam will be Monday June 11 • Possibility – Tuesday June 5, limited lecture, and I answer questions in class about the project. 6/16/2021 2

Cse 322, Programming Languages and Compilers Runtime. c #include <stdio. h> extern int Mini_main(char*

Cse 322, Programming Languages and Compilers Runtime. c #include <stdio. h> extern int Mini_main(char* s); int main() { printf("Entering main in mini. Java programn"); Mini_main("123"); return 0; } void pr. String(char* s) { printf(s); printf("n"); } void pr. Int(int n) { printf("%dn", n); } 6/16/2021 3

Cse 322, Programming Languages and Compilers Assumptions • The first assumption is that the

Cse 322, Programming Languages and Compilers Assumptions • The first assumption is that the only visible name in the assembly code is Mini_main • runtime functions are in runtime. c – e. g. pr. Int and pr. String – these can be accessed as _pr. Int and _pr. String in assembly – E. g. pushl call addl 6/16/2021 underscore before the name of the C function %eax _pr. Int # pr. Int((MEM(V 4) / 2)) $4, %esp 4

Cse 322, Programming Languages and Compilers Running the code. • The compiler will write

Cse 322, Programming Languages and Compilers Running the code. • The compiler will write a file in assembly format. • The driver I supply will have a function that given a file name and a (IA 32 list) will format the instructions and print them to the file. – E. g. IA 32. print. Ia 32 "work/obj. s" x 86 • You should write the file into the same directory where runtime. c is • Then you should use gcc to compile – gcc runtime. c obj. s • Then you should run the program –. /a. exe 6/16/2021 5

Cse 322, Programming Languages and Compilers What if something goes wrong? • If you

Cse 322, Programming Languages and Compilers What if something goes wrong? • If you have the println working in mini. Java you can put in println to see where you are before it goes wrong. – So you may want to get this working first! (hint, Hint !!! ) • You can manually inspect the code, by looking at the generated file. Of course a good translator will put comments in the file appropriately. • You can try and run the code in a debugger. 6/16/2021 6

Cse 322, Programming Languages and Compilers Debugging Assembly • • • In order to

Cse 322, Programming Languages and Compilers Debugging Assembly • • • In order to get things correct you may want to debug assembly. I have successfully used the gdb debugger Resources – The web page http: //www. csee. umbc. edu/~cpatel 2/links/310/nasm/gdb_help. shtml was very helpful to me • Under Linux, and Cygwin on Windows, the complete documentation for gdb is available on the Linux system using the "info" command: – info gdb 6/16/2021 7

Cse 322, Programming Languages and Compilers Over all view • To debug • Compile

Cse 322, Programming Languages and Compilers Over all view • To debug • Compile with the –g option – gcc –g runtime. c obj. s • Then start the executable with the debugger – gdb a. exe • Inside the debugger run the code 6/16/2021 8

Cse 322, Programming Languages and Compilers Accessing variables • Variables in the assembly code

Cse 322, Programming Languages and Compilers Accessing variables • Variables in the assembly code have _x • but in the debugger the _ is not used • Registers are $eax $ebx $esp etc 6/16/2021 9

Cse 322, Programming Languages and Compilers Running code • start • step (s) –

Cse 322, Programming Languages and Compilers Running code • start • step (s) – 1 step (either C or assembly) – goes into function calls • next (n) – 1 step (either C or assembly) – goes over function calls 6/16/2021 10

Cse 322, Programming Languages and Compilers Break commands • break Mini_main • break 12

Cse 322, Programming Languages and Compilers Break commands • break Mini_main • break 12 – break at line 12, useful when in the C code 6/16/2021 11

Cse 322, Programming Languages and Compilers Printing things • print takes a format •

Cse 322, Programming Languages and Compilers Printing things • print takes a format • print/d x • print/x temp – – – – – 6/16/2021 o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), i(instruction), c(char) and s(string). 12

Cse 322, Programming Languages and Compilers X examining things • x/count-format-size object • x/4

Cse 322, Programming Languages and Compilers X examining things • x/count-format-size object • x/4 dw temp – print temp and the next 3 things of word size in decimal format • This is useful for examining the stack • x/8 dw $esp – print the top 8 words of the stack – works because the stack grows downwards 6/16/2021 13

Cse 322, Programming Languages and Compilers info (gdb) info reg eax ecx edx ebx

Cse 322, Programming Languages and Compilers info (gdb) info reg eax ecx edx ebx esp ebp esi edi eip eflags cs ss ds es fs gs 6/16/2021 0 x 401075 4198517 0 x 0 0 0 x 4 c 76 0 x 4 4 0 x 22 ee 50 0 x 22 ee 68 0 x 4621 ac 4596140 0 x 61102 c 1 c 1628449820 0 x 401075 0 x 206 518 0 x 1 b 27 0 x 23 35 0 x 38 56 0 x 0 0 14

Cse 322, Programming Languages and Compilers Handling division • Division is tricky because you

Cse 322, Programming Languages and Compilers Handling division • Division is tricky because you must have things in the correct registers. • to execute (x / y) • get x into %eax • clear %edx -- as long as x is positive – movl $0, %edx • get y into %ebx • Do the divide – idivl %ebx • Divides (%edx: %eax) as a 64 bit by %ebx – result in %eax – remainder in %edx 6/16/2021 15

Cse 322, Programming Languages and Compilers Handling strings for println • We need to

Cse 322, Programming Languages and Compilers Handling strings for println • We need to allocate strings at the beginning of the assembly file. S_4: S_3: S_2: S_1: S_0: 6/16/2021 . text. asciz. globl "a && b = 0" "y/2 = 2" "y*z = 12" "y-z = 1" "x+y = 4" _Mini_main 16

Cse 322, Programming Languages and Compilers ML algorithm val string. Count = ref 0;

Cse 322, Programming Languages and Compilers ML algorithm val string. Count = ref 0; val strings = ref ([]: IA 32 list) fun new. String s = let val n = !string. Count val _ = string. Count : = n+1 val label = "S_"^Int. to. String n in strings : = (Asciz(label, s, “literal") ): : (! strings); label end; 6/16/2021 17

Cse 322, Programming Languages and Compilers Translating fun compile. E exp = case exp

Cse 322, Programming Languages and Compilers Translating fun compile. E exp = case exp of STRING(s) => let val label = new. String s in [ movl (Lit label, % eax) ] end 6/16/2021 18

Cse 322, Programming Languages and Compilers Collecting fun compile. Program xs = let val

Cse 322, Programming Languages and Compilers Collecting fun compile. Program xs = let val _ = string. Count : = 0 val _ = strings : = [] val body = List. concat (map compile. Func xs) in [ text() ] @ !strings @ body end; 6/16/2021 19

Cse 322, Programming Languages and Compilers Testing your compiler • The strategy to test

Cse 322, Programming Languages and Compilers Testing your compiler • The strategy to test your compiler is to build small minijava files that exercise 1 feature. • Then – – 6/16/2021 compile observe translation run through gcc run test file. 20

Cse 322, Programming Languages and Compilers My first mini. Java program class test 01

Cse 322, Programming Languages and Compilers My first mini. Java program class test 01 { public static void main(String[] a) { System. out. println("TEST 01"); System. out. println(123); System. out. println(true); } } 6/16/2021 21

Cse 322, Programming Languages and Compilers Testing arithmetic class test 01 { public static

Cse 322, Programming Languages and Compilers Testing arithmetic class test 01 { public static void main(String[] a) { int x = 0; int y = 4; int z = 3; boolean a = true; boolean b = false; System. out. println("x+y = 4"); System. out. println(x+y); System. out. println("y-z = 1"); System. out. println(y-z); System. out. println("y*z = 12"); System. out. println(y*z); System. out. println("y/2 = 2"); System. out. println(y/2); System. out. println("a && b = 0"); System. out. println(a && b); } } 6/16/2021 22

Cse 322, Programming Languages and Compilers http: //www. cygwin. com/ 6/16/2021 23

Cse 322, Programming Languages and Compilers http: //www. cygwin. com/ 6/16/2021 23