Lecture%2024%20-%20C%20and%20Assembly

Lecture 24 C - Ananda Gunawardena Lecture 31 C and Assembly High level languages are designed to allow programmers to write programs in a way that

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Ananda Gunawardena Lecture 31 C and Assembly High level languages are designed to allow programmers to write programs in a way that is closer to the logical thinking of the program flow. However, programming in assembly language requires programmers to understand the instruction set architecture of the machine in addition to the logical program flow. Writing a program in machine language or assembly language is like programming a microprocessor kit. It requires the understanding of low level details of how a machine may execute a set of instructions and the fetch-execute cycle among other things. Today most programmers don’t deal directly with assembly language, unless the task requires direct interfacing with hardware. For example, a programmer may consider using an assembly language to write a device driver or optimize part of a game program using assembly code. To understand the assembly code, Let us consider the simple code below. #this is in the file first.s .global main main: movl $20, %eax movl $10, %ebx ret The first line of the program is a comment. The .global assembler directive makes the symbol main visible to the linker. This line makes the program linked up with the C start up library. If we try to remove this line, then we get the following message % gcc first.s /usr/lib/crt1.o: In function `_start’: : undefined reference to `main’ collect2: ld returned 1 exit status The commands like movl $20, %eax means that the bit pattern of 20 is moved to register eax. Since the inception of high level programming languages like cobol and fortran, there are only a handful of instances where a programmer need to write an assembly program. Modern compilers make it easier for us to develop, test and debug programs. Compilers convert high level programs written in a language like C into assembly code. Using the GNU C compiler –S option, we can generate the assembly code for a source code. For example, consider the program simple.c
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include <stdio.h> int main(){ int x=10,y=15; return 0; } The purpose of the above program is to define and initialize two variables x and y. When the program is compiled with –S option h gcc – S first.c it produces the assembly code file simple.s as shown below. .file "first.c" .text .globl main .type main, @function main: pushl %ebp -- save a copy of ebp in stack movl %esp, %ebp -- move esp to ebp subl $8, %esp -- reserve space on stack for two values andl $-16, %esp -- alignment operation movl $0, %eax -- move 0 to register eax
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/27/2009 for the course CS 123 taught by Professor Bajkzek during the Fall '08 term at Carnegie Mellon.

Page1 / 7

Lecture 24 C - Ananda Gunawardena Lecture 31 C and Assembly High level languages are designed to allow programmers to write programs in a way that

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online