Homework • Reading • Programming Assignments

advertisement
Homework
• Reading
– Finish K&R Chapter 1 (if not done yet)
– Start K&R Chapter 2 for next time.
• Programming Assignments
– Finish HW1 assignment (if not done yet)
– Continue HW2 assignment
1
“Octal” Dump
• Use “od –x” to see hex dump of a file
od –x trim.in
00000000 0909 4e68 …. 2020
...
00000120 7061 7274 .… 0a0a
• Octal and Hexadecimal numbers
• Why dump in Hex instead of Octal?
• ASCII code for representing characters
2
Octal and Hex Numbers
• People normally deal in numbers base 10
• Computers normally deal in numbers base 2
• The problem:
– Reading a long string of 1’s and 0’s not easy
– Conversion between base 2 and base 10 not easy
• The solution:
– Convert binary digit strings to Octal or Hex
– Easily done because 23 = 8 and 24 = 16
3
Octal and Hex Numbers
• Look at a long string of binary digits in groups
– Group from right by 3 digits for Octal
– Group from right by 4 digits for Hex
• See the following examples:
–
–
–
–
–
Binary Digits
Grouped by threes
For Octal
Grouped by fours
For Hex
011010101100
011 010 101 100
0
3
2
5
4
0110 1010 1100
0x
6
a
c
• Don’t convert binary to/from Hex/Octal via decimal!
4
Octal and Hex Numbers
• Octal
OCTAL
0
1
2
3
(Octal Constant is written 0dd…)
BINARY
000
001
010
011
OCTAL
4
5
6
7
BINARY
100
101
110
111
• Note: Can’t write a decimal value with a
leading 0 digit – will be interpreted as octal
5
Octal and Hex Numbers
• Hex
HEX
0
1
2
3
(Hex Constant is written 0xdd…)
BIN.
0000
0001
0010
0011
HEX
4
5
6
7
BIN.
0100
0101
0110
0111
HEX
8
9
A
B
BIN.
1000
1001
1010
1011
HEX
C
D
E
F
BIN.
1100
1101
1110
1111
• NOTE: Memorize these translations
• DO NOT convert between binary and Hex or Octal
by converting to decimal and back! Much harder!!
6
ASCII Code
• For computers to process our letters, digits,
punctuation marks, etc, we need a binary code
for each such “character”.
• American Standard Code for Information
Interchange (ASCII) provides these codes.
• See ASCII code chart (from Lecture 2)
• Standard 8 bit bytes and 16 bit words are not
integer multiples of 3 bits but are integer
multiples of 4 bits – favoring use of Hex!
7
ASCII Codes in visitype.c
• To convert a character’s (integer) value to a
printable ASCII string for what it represents:
– We need a table of 4 byte character strings each
ending in a zero byte (created by using “\0”)
– Arranged in the order of the character values
used in the ASCII code to represent them
– And to create the address of one of these strings
to pass to function printf e.g. &asciiname[4*i]
8
Analysis of visitype.c
• See separate program text “visitype.c”
• ASCII code conversion array
char asciiname [] = …. ;
• Initialization values for the array
128 strings of length 4 including the ‘\0’
asciiname[0]
‘N’
…[1]
…[2]
…[3]
…[4]
…[5]
…[6]
‘U’
‘L’
‘\0’
‘S’
‘O’
‘H’
…[7]
…
‘\0’
…
9
Analysis of visitype.c
• There are a few characters which can't be placed in the
quoted initialization string directly, for example " and \.
"∆∆"\0"
(where " is hex 22)
• Problem is that " as a character will be interpreted as the end
of string. We need to indicate that it is quoted, that it is to
be taken literally by the compiler as an ASCII value, and the
way to do that is precede it by a \.
"∆∆\"\0" (where " is hex 22)
• Now there seem to be five chars in that string, “∆∆\"\0”
• But there are not, \" is a single character value, just as \0
is
10
Analysis of visitype.c
• What other characters MAY need the same
special treatment?
– See K&R page 193.
– Good page to mark for open book tests!
• Do all of these require special treatment here?
– No, which one other than \“ needs it?
– Answer: (write in here) _________
11
Analysis of visitype.c
• Access the strings in the array (i = 0 to 127)
asciiname[4*i]
• Array index 4*i indicates start of each string
• However, printf needs a pointer to a string
&asciiname[4*i]
• The “&” is the “address of” operator
– Take on faith for now - more on pointers later
12
More on Debugging
• How to debug. See Users Guide to Tools
http://www.cs.umb.edu/helproot/cs240/cs240.html
• A professional programmer uses a Debugger,
rather than putting in lots of printf statements to
track down a bug.
(Note: In some cases such as real time programs,
you can’t stop the program with a debugger, you
may need to use “printf” or “log” statements.)
13
Use of Debugger - gdb
• Start with the correct compiler options:
• gcc -g vt.c -o vt
-g for debug
-o replaces a.out with filename vt
• Now, instead of just typing program name:
• gdb vt
• Gives message:
Ready to run -- not yet running.
14
Use of Debugger - gdb
• Want to interact with running program, not just let
it run free to end. Type:
b main
break at main()
• To run the program via the debugger
r <vt.in
run, taking stdin from vt.in
• Will stop when encounters main() in program
execution -- often lot of things done first.
• Now can single step through program, s or n (skip
entering functions), put out values of variables.
15
Use of Debugger - gdb
• Examples of gdb commands:
p i(print value of variable i)
p 3*i
(print value of expression 3*i)
p/x i
(print in hex format value of variable i)
i lo
("info" - give values of all local variables)
h (help -- pretty good messages -- lists topics)
h topic (help on named topic)
h p
(help on command p for printf)
q TO QUIT (leave debugger)
16
Use of Debugger - gdb
• More complex gdb commands in UNIX Guide.
• Setting breaks/conditional breaks at line numbers:
b 36
b fn.c:22 if i == 3
• Getting line numbers from "list" or "l" command:
l 22
l fn.c
ib
d3
c
print 10 lines around line 22 in main
list first five lines, then l means next 10 lines
to get info on breakpoints
to delete bkpt 3
for continue after bkpt encountered
17
Debugging “core” files
• If you compile a program with –g option, run it,
and it crashes, e.g. “segmentation fault – core
dumped”, you can use gdb to the analyze the
state of the program at the point where it crashed.
• Use the gdb command with two parameters:
gdb <program file name> <core file name>
• Example:
gdb buggy core
• Use all gdb commands as usual – except step,
next, or continue since the program has already
crashed. Display variable values, etc. to debug.
18
Download