Assembly Language Question

Learning Outcomes

Explain the difference between characters and numbers on a computer.

Use an array as a lookup table to check for character equality.

Use an algorithm to translate a number from a different base to decimal.

Develop a program that utilizes this algorithm.

Specifications

For this assignment you will write a program that can convert numbers written in bases 2 to 16 into their decimal equivalents. The numbers will be prefixed with 0b, 0q, 0s, 0n, 0z and 0x, for example, indicating which base the given input is in.

There’s no need to write any code outside of lines 20-27 inside main() and the chars_to_int() function.

Arrays

Don’t worry about syntax such as char*, the chars variable is an array of characters. Python sort of has this same concept, but it hides the true nature of strings from you (they are just arrays of characters). You can use the same list subscript notation you learned in Python to access individual elements of the chars array. The elements in the array are of type char which is just an 8-bit number. All values stored inside of a computer are binary numbers.

When you see char *chars you can read this as “a pointer to a char” which may or may not be followed in memory by more characters. For our program, we know that chars is really an array of characters ending with a value of 0, also called a null-terminator. We should have learned in the intro courses that an array is contiguous memory. You should keep in mind that the value stored in the chars variable is a memory address where a character lives and the memory addresses in front of that one contains more characters until a location where there is the value 0.

Characters Are Numbers

The other important concept in this assignment is that the characters you see on the screen of a computer are just numbers. A CPU cannot store anything other than 1s and 0s, so we have decided that certain values should represent characters so that we can store letters and words. To see which values represent which letters you should execute man ascii in a terminal (or ). Notice that numbers and letters are in contiguous blocks in this table. Since characters are just numbers, you can perform math on them. You must perform math on the elements in the chars array in order to calculate the resulting value.

Restrictions

You may not use any functions from the standard library at all except for fprintf() to display errors to the user. If you’re calling a function other than fprintf() or chars_to_int() then you are violating this restriction.

You may not declare other functions.

You may not modify code outside of lines 20-27 inside main() and the chars_to_int() function.

Submission

Submit only your completed bd.c file. You can make a submission as many times as you’d like in order to see the autograder feedback. The maximum points you can receive from the autograder is 16 and the remaining 12 points are from code review.

Example

Here’s a sample interaction with a working program. Your program must not print out anything other than the number or the autograder will fail. The $ character represents the terminal prompt, you do not type this character when executing commands.

$ ./bd 0b111014$ ./bd 0q3321249$ ./bd 0s523195$ ./bd 0n808656$ ./bd 0zAB11573$ ./bd 0xCAFE51966