{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Lecture%2024%20-%20C%20and%20Assembly - Ananda Gunawardena...

Info icon This 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
Image of page 1

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

View Full Document Right 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 head2right gcc – S first.c it produces the assembly code file simple.s as shown below.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern