Warning

This page is located in archive.
Go to the latest version of this course pages.
Go the latest version of this page.

- for lecturer: cvičení 2

- basic data types (integer number) storage in computer memory, program in C
- integer representation, addition, subtraction, multiplication, division
- real number represetation (in IEEE 754)

- binary representation and hexadecimal numbers
- C language syntax
- terms little and big endian
- the second complement code
- logic operations (and, or, invert, rotation, …)
- two's complement code and IEEE 754.
- to understand the program from the last class.

The exercise will be based on following C code. It prints number representation in computer memory. We shall modify the code during the class.

#include <stdio.h> #define PRINT_MEM(a) print_mem((unsigned char*)&(a), sizeof(a)) void print_mem(unsigned char *ptr, int size) { int i; printf("address = 0x%08lx\n", (long unsigned int)ptr); for (i = 0; i < size; i++) { printf("0x%02x ", *(ptr+i)); } printf("\n"); } int main() { unsigned int unsig = 5; int sig = -5; printf("value = %d\n", unsig); PRINT_MEM(unsig); printf("\nvalue = %d\n", sig); PRINT_MEM(sig); return 0; }

To compile the program: gcc -Wall -ansi -pedantic ./program.c

Python language alternative

#!/usr/bin/python3 import struct a = 1234.56789 b = -11.1111111111111111111111 c = a + b buf = struct.pack('<f', c) print ('C float LE:' + ' '.join(["{0:02x}".format(b) for b in buf])) print ('C float LE:' + str(struct.unpack('<f', buf)[0])) buf = struct.pack('>f', c) print ('C float BE:' + ' '.join(["{0:02x}".format(b) for b in buf])) print ('C float BE:' + str(struct.unpack('>f', buf)[0])) buf = struct.pack('<d', c) print ('C double LE:' + ' '.join(["{0:02x}".format(b) for b in buf])) print ('C double LE:' + str(struct.unpack('<d', buf)[0])) buf = struct.pack('>d', c) print ('C double BE:' + ' '.join(["{0:02x}".format(b) for b in buf])) print ('C double BE:' + str(struct.unpack('>d', buf)[0]))

See Python struct module for more struct — Interpret bytes as packed binary data.

- Compile and execute program above
- Explain output of the program
- Alter the code to print representation of other data types (float, char, double, long, etc…)
- Alter the code to print numbers and their representation between -16…15.
- Modify the program to add and subtract integer numbers and print arguments and results (in “normal” form and computer representation).
- Try to add and subtract negative and positive numbers. Try to cause integer overflow and underflow.

- Integer addition and subtraction in two's complement representation
- add and subtract two integer numbers. For example 5+(-6) and 5-(-6).
- repeat the operations with different numbers and check your results with the computer program from the first exercise.
- When the underflow and overflow can happen? How can we detect, that it had occured?

- Integer multiplication
- multiply two integers, For example 7*6.
- is there any difference when multiplying negative numbers? (e.g. -7*6, (-7)*(-6), etc…)
- show how to speed-up the multiplier? (use many adders instead repetitively using one).

- Integer division
- divide integers 42/7, 43/7
- does the algorithm change when we use negative numbers?

- Real number representation (IEEE 754)
- binary representation of real numbers (float - 32bit, double - 64bit)
- show binary representation of -0.75. Check your result with program from the last exercise.
- find decimal number for float binary number 0xC0A00000 in IEEE754.
- explain who to add numbers 9.999*10^1 and 1.1610*10^(-1) in decimal representation. Assume that it is possible to store only 4 digits in mantisa and 2 digits in exponent.
- Hint: 1) align numbers 2) addition 3) normalization 4) round numbers.

- in binary representation add 0.5 and -0.4375
- in decimal representation show multiplication of 1.110*10^10 * 9.200*10^(-5)
- in binary representation multiply 0.5 and -0.4375

- [ http://www2.its.strath.ac.uk/courses/c/ ] - (Short) Introduction to C language
- [ http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html ] -
**printf**function documentation. The printf is used for formated output. - Description of IEEE 754 numbers representation on Wikipedii

courses/b35apo/en/tutorials/02/start.txt · Last modified: 2022/01/30 09:43 (external edit)