F28HS2 Hardware-Software Interface Lecture 12: Assembly Language 6 & Summary.

Slides:



Advertisements
Similar presentations
the c language BY SA 1972 by Ken Thompson and Dennis Ritchie.
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
1 Compiler Construction Intermediate Code Generation.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
F28PL1 Programming Languages Lecture 20: Summary.
F28PL1 Programming Languages Lecture 5: Assembly Language 4.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
Run time vs. Compile time
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
Guide To UNIX Using Linux Third Edition
ARM C Language & Assembler. Using C instead of Java (or Python, or your other favorite language)? C is the de facto standard for embedded systems because.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Imperative Programming
F28PL1 Programming Languages Lecture 10: Programming in C 5.
F28PL1 Programming Languages Lecture 7 Programming in C - 2.
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Names Variables Type Checking Strong Typing Type Compatibility 1.
C Programming language
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Introduction to Computer Programming Using C Session 23 - Review.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Programming Languages and Paradigms Imperative Programming.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C LANGUAGE Characteristics of C · Small size
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Arrays and Strings in Assembly
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
F28HS Hardware-Software Interface Lecture 11: ARM Assembly Language 5.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Writing Functions in Assembly
Chapter 7: User-Defined Functions II
C Primer.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
A bit of C programming Lecture 3 Uli Raich.
Writing Functions in Assembly
Chapter 9 :: Subroutines and Control Abstraction
ARM Assembly Programming
Instructions - Type and Format
Procedure Activations
Lecture 2 SCOPE – Local and Global variables
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
15213 C Primer 17 September 2002.
Presentation transcript:

F28HS2 Hardware-Software Interface Lecture 12: Assembly Language 6 & Summary

Library calls to call functions in C libraries procedure call standard depends on architecture follow AAPCS – Procedure Call Standard for the ARM Architecture pass parameters 1-4 in R0-R3 pass other parameters on stack

Library calls need to make assembly program look like C change _start to main.global function - for each library function we wish to call

Library calls compile with as as before – as will generate _start from main link with gcc instead of ld gcc automatically links to libc – C standard library NB don’t mix system calls & library calls – different call conventions

Example: Q & A main() { int n; printf(“How many beans make 5?”); scanf(“%d”,&n); if(n==5) printf(“Well done!\n”); else printf(“%d beans do not make 5!”,n); }

Example: Q & A....global printf.global scanf.data f1:.asciz "How many beans make 5? " f2:.asciz "%d" f3:.asciz "Well done!\n" f4:.asciz "%d beans do not make 5!\n" n:.word 0x00

Example: Q & A.global main main: LDR R0, =f1 BL printf LDR R0, =f2 LDR R1, =n BL scanf LDR R0, =n LDR R1, [R0] CMP R1, #5 BEQ yes no: LDR R0, =f4 B print yes: LDR R0, =f3 print: BL printf B _exit...

Example: Q & A $ as -g -o qa.o qa.s $ gcc -o qa qa.o $./qa How many beans make 5? 4 4 beans do not make 5! $

Example: file copy main(int argc,char ** argv) { FILE * fin, * fout; int ch; fin = fopen(argv[1],"r"); fout = fopen(argv[2],"w"); ch = getc(fin); while(ch!=EOF) { putc(ch,fout); ch = getc(fin); } fclose(fin); fclose(fout); }.global printf.global.fopen.global.fclose.global getc.global putc.data fin:.word 0x00 fout:.word 0x00 r:.asciz "r" w:.asciz "w"

Example: file copy main(int argc,char ** argv) { FILE * fin, * fout; int ch; fin = fopen(argv[1],"r"); fout = fopen(argv[2],"w"); ch = getc(fin); while(ch!=EOF) { putc(ch,fout); ch = getc(fin); } fclose(fin); fclose(fout); }.global main fopen input argv[1] PUSH {R1} LDR R0, [R1,#0x04] LDR R1, =r BL fopen LDR R1, =fin STR R0, [R1] NB main is a function call argc in R0 argv in R1

Example: file copy main(int argc,char ** argv) { FILE * fin, * fout; int ch; fin = fopen(argv[1],"r"); fout = fopen(argv[2],"w"); ch = getc(fin); while(ch!=EOF) { putc(ch,fout); ch = getc(fin); } fclose(fin); fclose(fout); fopen output argv[2] POP {R1} LDR R0, [R1,#0x08] LDR R1, =w BL fopen LDR R1, =fout STR R0, [R1]

Example: file copy main(int argc,char ** argv) { FILE * fin, * fout; int ch; fin = fopen(argv[1],"r"); fout = fopen(argv[2],"w"); ch = getc(fin); while(ch!=EOF) { putc(ch,fout); ch = getc(fin); } fclose(fin); fclose(fout); } loop:LDR R1, =fin LDR R0, [R1] BL getc CMP R0, #-1 BEQ endl LDR R2, =fout LDR R1, [R2] BL putc B loop endl:

Example: file copy main(int argc,char ** argv) { FILE * fin, * fout; int ch; fin = fopen(argv[1],"r"); fout = fopen(argv[2],"w"); ch = getc(fin); while(ch!=EOF) { putc(ch,fout); ch = getc(fin); } fclose(fin); fclose(fout); fclose files LDR R1, =fin LDR R0, [R1] BL fclose LDR R1, =fout LDR R0, [R1] BL fclose _exit:MOV R7, #1 MOV R0, #0 SWI 0

Summarising language characteristics how does a language abstract away from underlying byte machines? – types – data abstraction – control abstraction what are pragmatic consequences of language characteristics? – i.e. how do characteristics affect use?

Types values & operations what are base types? – e.g. Java: int, float, char etc what are structured types? – e.g. Java: object, array, String etc

Types how do types constrain language? weak v strong typing – i.e. whether type associated with entity can (weak) or can’t (strong)change – e.g. Java: strong typing static v dynamic typing – i.e. whether types checked at compile time (static) or run time (dynamic) – e.g. Java: static typing

Polymorphism type abstraction – can types be generalised? – polymorphism == many shapes ad-hoc v parametric polymorphism – ad-hoc == “for this” i.e. language/context specific – parametric == controlled by parameters

Polymorphism e.g. Java – ad-hoc polymorphism operator overloading i.e. can use some operators with different types e.g. arithmetic – parametric polymorphism i.e. generic types with type variables

Data abstraction memory abstraction variable as name/value abstraction from address/contents – e.g. Java: variables where may variables be introduced? – e.g. Java: class fields, method formal parameters, block/method bodies, iteration control

Data abstraction how may variables be bound to values? e.g Java: – initialising declaration – assignment – parameter passing

Data abstraction scope – i.e. where is something visible? – lexical v dynamic scope – i.e. constrained or not by site of definition/declaration extent – i.e. how long does something exist?

Data abstraction e.g. Java: lexical scope – class field/method names visible via object – variables in block/ method body visible in block unless redefined – method formal parameter visible in method body only e.g. Java: block extent – variables only exist in defining block/method body

Control abstraction structured operations as commands how are calculations performed? – e.g. Java: expression how is memory accessed? – e.g. Java: use variable name in expression context how is memory changed? – e.g. Java: assignment to variable

Control abstraction how are commands structured? e.g. Java: – sequence block, nested method calls – choice if, switch – repetition while, for, iterators, recursion

Control abstraction e.g. Java – control flow method call, return & break – procedural void method i.e. from control sequences – functional method with return type i.e. from expressions – call by reference parameter passing

Program abstraction encapsulation – abstract over data & control e.g. Java – classes/objects

Pragmatics what is mapping to byte machine? how implemented? how easy to read/write/debug? performance? use? etc...

C summary: types strong, static types – but... can override types with casts & address manipulation base types – int, short, long, char, float, double structured types – array, struct, pointer, union, function ad-hoc polymorphism – operator overloading & cast

C summary: data abstraction variable – name/value association – abstracts address/contents can still expose low level memory: – & and * – can request that variable be bound to register

C summary: data abstraction variable introduction – declaration at start of program – declaration at start of block – formal parameters scope – lexical extent – block

C summary: control abstraction expressions – abstract from arithmetic/logic sequences commands – abstract from: memory/register manipulation sequences flag test, branch & address

C summary: control abstraction commands – assignment – sequence block & function body – choice if & switch – repetition for & while – flow of control function call, return, break & goto

C summary: control abstraction functions – functional abstraction with return type – procedural abstraction no return type – call by value & by reference parameter passing

C summary: pragmatics in-between assembler & high-level one to many mapping from command to machine code must be compiled to machine code (or interpreted) easier to read/write/debug than assembler can manipulate low level byte machine weak typing  errors from pointers & casts

C summary: pragmatics must manage memory explicitly not as fast as assembler CPU independent – mostly portable – but size of type depends on implementation used for system level programming, real- time/embedded control rapidly replacing assembler for lower-level programming

Assembler summary: types no types – everything is a byte representations for: – numbers, characters etc no type checking – representation are conventional – can apply any operation to any byte sequence regardless of representation – i.e. ad-hoc polymorphism

Assembler summary: data abstraction names R0-R15, PC, LR, SP etc abstract from CPU registers labels abstract from memory addresses names and labels used as variables i.e. use name as operand to access/change register/memory no data structures – must craft explicitly from byte sequences

Assembler summary: data abstraction registers: – scope/extent - whole program labels: – scope whole file + where imported – extent – whole program

Assembler summary: control abstraction operators abstract from machine code must craft structured constructs from operator sequences no universal standards or conventions – but compilers/operating will define standards – e.g. for parameter passing

Assembler summary: pragmatics direct machine code abstraction 1 to 1 mapping from assembly language to machine code easier to program than machine code must be assembled to machine code very fast – same as machine code

Assembler summary: pragmatics hard to read/write/debug CPU specific – not portable – 1950s attempts at universal machine code (UNCOL) failed used for mission/system critical software – e.g. device drivers, BIOS, small embedded systems