lecture 4
play

Lecture 4 Machine Code 3. Hardware and Softw are 4. High Level - PDF document

1. Introduction 2. Binary Representation Lecture 4 Machine Code 3. Hardware and Softw are 4. High Level Languages 5. Standard input and output As we have seen, programs exist as binary 6. Operators, expression and statem ents


  1. 1. Introduction 2. Binary Representation Lecture 4 Machine Code 3. Hardware and Softw are 4. High Level Languages 5. Standard input and output • As we have seen, programs exist as binary 6. Operators, expression and statem ents machine code. This is inconvenient for humans 7. M aking Decisions as we can not naturally interpret the code. 8. Looping • To overcome this, assembly language was 9. Arrays invented, e.g. 10. Basics of pointers LDA 1BC9 - load accumulator from address 1BC9 11. Strings ADD 05 - add 5 to accumulator 12. Basics of functions STA 1AF7 - store the result at address 1AF7 13. M ore about functions • This is translated more or less directly into 14. Files machine code by a program called an 14. Data Struc tures assembler 16. Cas e study: lottery num ber generator Assembler Compilers Machine Code Executable Object file files HLL Program Machine Code Source File Assembler Object File Linker Object (assembler code) (program) (machine code) Source file Compiler Program file • For complex programs we need a greater level • A compiler is used to convert the HLL source code of abstraction which is offered by high level into an object file, which is then possibly combined languages such as c with others to produce an executable program. e.g. • Other object files can come from other programmers, x=newWindow(200,300,x_pos,y_pos); libraries (e.g. maths, graphics), assembler or other y=1+sin(theta)-cos(phi)/2; HLLs Anatomy of a Simple C Program <stdio.h>, which deals with input and output. For instance, /* This is a function definition. Every C program needs a /* Declaring two local variables, visible only within the /* Note: C doesn't allow nested comments: you can't put one it declares the function printf, for formatted printing. */ /* It's useful to put comments in your programs to explain Errors what you're doing. Not too few, not too many: one helpful Most C programs need to include the standard header file The symbol ANSWER will be replaced by 42. It's useful for /* Function prototypes. Their actual definitions follow (global) variables, accessible to all functions in this file. There are good reasons for minimising the use of comment every 10 to 20 lines of code is probably about /* Declared outside any function, these are external • Several types of error can occur when developing programs. They are all introduced by the programmer, not the computer! /* Example: anatomy of a simple C program. */ /* This is another preprocessor directive. main function, where execution starts. */ right. This program has far too many! */ /* This is a preprocessor directive. int mult_and_print(float f, float g); – Compile time errors (reported by the compiler) a) Syntax errors, e.g. missing semicolon at end of statement b) Semantic errors, e.g. forgetting to #include <stdio.h>, then using printf (which therefore doesn’t mean anything) comment inside another! */ – Run time errors defining constants. */ float x, y = 13.7; the program compiles ok but crashes when it is ran, e.g. z=x/y; where #include <stdio.h> main function */ #define ANSWER 42 y=0 void add(void); – Algorithmic errors globals! */ int a, b; the program compiles and runs ok, but doesn’t do what is intended. main. */ main() { 1

  2. Anatomy of a Simple C Program • +1 • 127= • So when overflow occurs +127+1 gives?? Two’2 Complement • Whats the value of x now ? • Consider an unsigned int variable. Suppose Integers cont…... the compiler uses 2 bytes for this data type. 0 -128 Its 0000 (hex) - overflow has occurred. e.g. It can vary from 0000 to FFFF (hex), i.e. 0 to 65535 (decimal) 1 -128 1 64 0 64 x = y * ANSWER + a; /* This is a statement */ add(); 1 32 b = mult_and_print(x, y); /* These are function calls */ 0 32 return; /* End of the main function */ x=x+1; unsigned int x=65535; } 1 16 0 16 void add(void) /* Definition of a function */ { a = a + b; return; } 0 8 1 8 int mult_and_print(float f, float g) /* Definition of a function */ 1 4 { 0 4 int r; /* Declaring a local variable */ r = (int) (f * g); /* A statement */ 1 2 printf("Yesterday I saw %i penguins!\n", r); 0 2 /* A function call; the prototype for printf is in <stdio.h> */ return r; /* Returning a value to the calling function */ 1 = 127 1 0 = -128 1 } overflow.c /* Example: demonstrates overflow of integer variables */ • Two’s Complement, Invert the number and add 1 • One’s Complement, Invert the number • Standard Binary number • Negative integers are stored in a form known as One and Two’2 Complement • NB. C does not define how many bytes each type • The integer and character types can be either signed • C has only a few simple data types: Simple Data types in C /* Don't worry about how the program works, just appreciate what happens! */ one and two’s complement uses. The compiler does !!! or unsigned 1 -128 1 -127 0 128 – Character types – Floating point types: – Integer types #include <stdio.h> #include <limits.h> char float, double, long double int, short, long void main(void) { unsigned int x = UINT_MAX - 1; 1 64 1 64 0 64 signed int y = INT_MAX - 1; printf("x is an unsigned int, occupying %i bytes.\n\n", sizeof(x)); printf("The initial value of x is %u\n", x); x++; 1 32 1 32 0 32 printf("Add 1; the new value of x is %u\n", x); x++; printf("Add 1; the new value of x is %u\n", x); x++; printf("Add 1; the new value of x is %u\n", x); 1 16 1 16 0 16 printf("\ny is a signed int, occupying %i bytes.\n\n", sizeof(y)); printf("The initial value of y is %i\n", y); y++; printf("Add 1; the new value of y is %i\n", y); y++; 0 8 0 8 1 8 printf("Add 1; the new value of y is %i\n", y); y++; printf("Add 1; the new value of y is %i\n", y); return; } 1 4 1 4 0 4 x is an unsigned int, occupying 4 bytes. The initial value of x is 4294967294 Add 1; the new value of x is 4294967295 1 2 1 2 0 2 Add 1; the new value of x is 0 Add 1; the new value of x is 1 y is a signed int, occupying 4 bytes. 1 = -9 1 0 = -9 1 1 = 9 1 The initial value of y is 2147483646 Add 1; the new value of y is 2147483647 Add 1; the new value of y is -2147483648 Add 1; the new value of y is -2147483647 2

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend