| CompOrg Fall 2003 Homework #2 FAQ | ||
| CompOrg Home | HW2 Description | ||
| Question: | I know that each byte can only contain values beteen 0 and 255, but printf seems to ignore what I tell it and print very large numbers. Why does printf ignore "%02x", and when I use "%u" why is it printing 4294967248 when I'm only giving it a single byte which can't be that large? |
| Answer: |
The real issue is that the value you are giving printf is being
extended to 32 bits (all integer type values are extended to 32 bits
when passed to printf), and in doing so is creating a 32 bit negative
number if the 8 bit value was negative. If you pass printf something that is an unsigned data type, then when it is extended to 32 bits the resulting 32 bit int will be unsigned (the leftmost 23 bits will be 0, which is what you want here). |
|   |   |
| Question: | What if the value of a memory location is larger than 255, how can we display this (the homework says that each memory location should be between 0 and 255)? |
| Answer: |
Each memory location is a byte. This means you need to use a
pointer to some C data type that is a single byte. In other words,
your pointer should be something like a |
|   |   |
| Question: | How I can set a pointer to a specific memory address? |
| Answer: |
Remember that a C pointer is really just a variable that holds an integer whose value will be used as an address. So all you have to do is convince the compiler to let you assign a value from an int to a pointer variable. You need to cast an int as some pointer type, for example: unsigned int x; ... somehow x gets the value you want to use as an address... char *p; p = (char *) x; |
|   |   |
| Question: | Every address I try to use results in SEGV, how do I know what addresses will be valid? |
| Answer: | The address of your program is the best one to use (when it is running, it must be located in memory at some valid addresses). The example on the homework assignment should work fine on freebsd.remote.cs.rpi.edu. In general you can use |
|   |   |
| Question: | Can I use Visual C++ for this assignment? |
| Answer: | No - we will test your code using gcc on freebsd.remote.cs.rpi.edu. Visual C++ is not available on the CS lab FreeBSD machines. |
|   |   |
| Question: | How do I know when the user presses Enter or hits 'q' ? |
| Answer: | We talked about the fgets function in class - this is all you
need. If the user simply presses Enter - fgets will return (and the
buffer you give fgets will have a newline |
|   |   |
| Question: | Will you give us code to convert from ASCII/hex to int, and from a char to an ASCII string holding the binary representation? |
| Answer: | No - this is part of what you are supposed to write. |
|   |   |
| Question: | Help! I don't know how to start! |
| Answer: | You may want to start with some of the sample code we
discussed in class, for example make a copy of the
Compile the program with Run the program with Once you have the program working, change it so that it reads a number
from the command line (argc,argv) instead of always calling
I'd suggest creating a function that will display any memory location (single byte) in hex, another for displaying in decimal, and another for displaying in binary. Your main program can use the format specified on the command line to decide which one to call. Handling input from the user can be developed by itself, write a program that uses fgets to read one line at a time from stdin until it finds a line that begins with 'q'. Once you have this working, you can add the appropriate code to your hw program. It might also be a good idea to write and test code that converts from an ASCII-encoded hex number (like you will get on the command line), to an int. Once you have this working by itself, move the code to your main program. In general, an approach that often works for people who have difficulty getting started is to forget about the complete assignement, and just start on something very small. Once you master getting a trival program running, add a little bit at a time. |
|   |   |