lect1a - ompilers CS31003 9 Computer Sc Engg IIT Kharagpur...

Info icon This preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1 & $ % Introduction Lect 1 Goutam Biswas
Image of page 1

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

View Full Document Right Arrow Icon
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 2 & $ % Programming a Computer Machine language program Assembly language program High level language program Lect 1 Goutam Biswas
Image of page 2
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 3 & $ % An Assembly Language Program: IA-32 #include <asm/unistd.h> #include <syscall.h> #define STDOUT_FILENO 1 .file "first.S" .section .rodata L1: .string "My first program\n" L2: .text .globl _start Lect 1 Goutam Biswas
Image of page 3

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

View Full Document Right Arrow Icon
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 4 & $ % _start: movl $(SYS_write), %eax movl $(STDOUT_FILENO), %ebx movl $L1, %ecx movl $(L2-L1), %edx int $128 movl $(SYS_exit), %eax movl $0, %ebx int $128 Lect 1 Goutam Biswas
Image of page 4
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 5 & $ % Preprocessor, assembler and Linker $ /lib/cpp first.S first.s $ as -o first.o first.s $ ld first.o $ ./a.out My first program Lect 1 Goutam Biswas
Image of page 5

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

View Full Document Right Arrow Icon
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 6 & $ % An Assembly Language Program: Intel-64 Xeon #include <asm/unistd.h> #include <syscall.h> #define STDOUT_FILENO 1 .file "first.S" .section .rodata L1: .string "My first program\n" L2: .text .globl _start Lect 1 Goutam Biswas
Image of page 6
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 7 & $ % _start: movl $(SYS_write), %eax movq $(STDOUT_FILENO), %rdi movq $L1, %rsi movq $(L2-L1), %rdx syscall movl $(SYS_exit), %eax movq $0, %rdi syscall Lect 1 Goutam Biswas
Image of page 7

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

View Full Document Right Arrow Icon
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 8 & $ % Preprocessor, assembler and Linker $ /lib/cpp first.S first.s $ as -o first.o first.s $ ld first.o $ ./a.out My first program Lect 1 Goutam Biswas
Image of page 8
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 9 & $ % Printing an Integer #define BUFF 20 void print_int(int n){ // print_int.c char buff[BUFF], zero=’0’; int i=0, saveN, j, k, bytes; saveN = n; if(n == 0) buff[i++]=zero; else{ if(n < 0) { buff[i++]=’-’; n = -n; } Lect 1 Goutam Biswas
Image of page 9

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

View Full Document Right Arrow Icon
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 10 & $ % while(n){ int dig = n%10; buff[i++] = (char)(zero+dig); n /= 10; } if(buff[0] == ’-’) j = 1; else j = 0; k=i-1; while(j<k){ char temp=buff[j]; buff[j++] = buff[k]; buff[k--] = temp; } } Lect 1 Goutam Biswas
Image of page 10
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 11 & $ % buff[i]=’\n’; bytes = i+1; __asm__ __volatile__ ( "movl $4, %%eax \n\t" "movl $1, %%ebx \n\t" "int $128 \n\t" : :"c"(buff), "d"(bytes) ) ; // $4: write, $1: on stdin } Lect 1 Goutam Biswas
Image of page 11

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

View Full Document Right Arrow Icon
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 12 & $ % Printing an Integer: main #include <stdio.h> #include "print_int.h" int main() { int n; printf("Enter an integer: "); scanf("%d", &n); print_int(n); return 0; } Lect 1 Goutam Biswas
Image of page 12
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 13 & $ % Creating a Library $ cc -Wall -c print_int.c $ ar -rcs libprint_int.a print_int.o $ cc -Wall -c main_print_int.c $ cc main_print_int.o -L. -lprint_int $ ./a.out Enter an integer: -123 -123 $ Lect 1 Goutam Biswas
Image of page 13

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

View Full Document Right Arrow Icon
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 14 & $ % Types of High-Level Languages Imperative Programming Language Functional Programming Language Logic Programming Language Object-Oriented Programming Language Scripting Language Lect 1 Goutam Biswas
Image of page 14
Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 15 & $ % Imperative Languages Close to von Neuman architecture x = x + 5; Read
Image of page 15

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

View Full Document Right Arrow Icon
Image of page 16
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