1 / 16

CS162B: Assembly and C

CS162B: Assembly and C. Jacob T. Chan. Objectives. System calls Relation of System calls to Assembly and C Special System Calls (exit, write, print, etc.) Lab 5. Processing…. Some programs take too long to run

kadeem
Download Presentation

CS162B: Assembly and C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS162B: Assembly and C Jacob T. Chan

  2. Objectives • System calls • Relation of System calls to Assembly and C • Special System Calls (exit, write, print, etc.) • Lab 5

  3. Processing… • Some programs take too long to run • Running from console terminal will prevent you from running other programs in the same terminal (until program exits) • Issue when system complains for running too much consoles running • Program vs process? • One solution: put & • Makes program run in background • BUT it does not make the program a daemon (more on this later on)

  4. Exit Code • In C/C++, main() method returns 0 (unless there was something wrong) • Same goes for Java’s System.exit() that takes in an intas an argument • This int is usually 0 (unless specified otherwise due to errors) • Exit code or return value is tested by whatever called the program (either by main() or by exit()) • Zero (0) = normal program termination • Non-zero = abnormal termination of program • Cause: ERRORS • Return value may give a hint of what type of error it is

  5. System Calls • Layer between user and kernel • Lowest interaction level with O/S • Entry point to the kernel • Should be provided by O/S • Everything else is built on top of the system call • Normally uses C/C++ • Not Java (because system calls only relay signals to JVM) • Java system calls result in OVERHEAD • And besides, JVM is a VIRTUAL MACHINE

  6. Categories of System Calls • Process Control • File Manipulation • Signals (obsolete; system calls rely on signals) • Device Management • Information Maintenance • Communications

  7. Categories of System Calls • System calls may fall on more than one category • chmod is both file manipulation and system maintenance • abort is process control and signal system call

  8. In relation to Assembly and C… • Function calls behave similarly for Assembly code • CS152a format of function calls is same in Assembly • GNU Assembler format will be used for this class • AT&T Syntax will be followed • Show assembly code in C programs • We will assume a 32-bit architecture (not 64-bit) • Might not work. Why?

  9. GAS • GNU Assembler files end with .S • Programs in C are converted to assembly then to binary during compiling

  10. Registers • ESP, EAX, EDP, etc. are the registers • Remember R0 to R31? • What is the register name for the stack pointer? The base pointer? • GAS code follows Beta assembly code (BSIM)

  11. Defining Functions in GAS... • This is just crash course Assembly (it’s gonna take a separate semester if so) • Defining a function myFunc .text .globlmyFunc .type myFunc, @function myFunc: # code follows # this is a comment

  12. System calls in GAS • Some functions provide kernel-mode only functionality • Just like sudo • To obtain such features, function must issue signal to O/S via interrupt • In Linux, this is achieved by typing int $0x80

  13. Example: Write function • Check out the mySysWrite.S (uploaded in Moodle) • Then add it to C code (and use it like a regular function) extern intmyFunc(intfildes,constvoid *buf, intnbyte); • Compiling the program gcc -o demoProgmyProg.cmyFunc.S

  14. Required: Knowledge • In making system calls, you need: • The number code to be inserted in the eax register before calling interrupt • Arguments to be placed in ebx, ecx, etc.

  15. Lab 5: Making Assembly Sys Calls • Implement system calls for opening, closing, reading, writing, and exiting using assembly in C • NOTE: writing is already demonstrated here in C • Write a C program copy.c that makes use of these system calls • This copies specified input file to a specified output file • DO NOT USE the stdio.h library functions for this one (printf/scanf) • Output file is to be named “output.txt”but for input files, assume it’s a .txt file

  16. Lab 5: Making Assembly Sys Calls • Include a Certificate of Authorship, your copy.c, and your .S files(open.S, write.S, read.S, close.S, exit.S) • Filename Convention: CS162B_Lab5_<Section>_<Surname>_<ID#>.tar • Deadline: 3 Days after today

More Related