CS 330 Lecture 16 – Assembly

Agenda

  • a little peek at GNU Assembly
  • code transformation
    • to preprocessed
    • to assembly
    • to object code
    • to linked executable
  • variables? no, registers
  • %ebp and %esp
    • on call and return
    • local variables
  • %eax for return values
  • passing arguments to functions
  • arrays
  • pointers
  • conditional statements
  • loops

Code

ls.c

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>

int main(int argc, char **argv) {

  for (int i = 0; i < argc; ++i) {
    printf("argv[%d]: %s\n", i, argv[i]);
  }

  char path[256];
 
  printf("Directory?\n");
  int result = scanf("%s", path);

  if (result == EOF) {
    printf("...... Okay.\n");
    exit(1);
  }

  DIR *dir = opendir(path);
  struct dirent *entry;
  while (entry = readdir(dir)) {
    printf("entry->d_name: %s\n", entry->d_name);
  }
  closedir(dir);

  /* free(dir); */
  /* free(entry); */

  return 0;
}

assembly.mk

%.s: %.c
	gcc -fno-asynchronous-unwind-tables -Wall -std=c99 -S -o $@ $<

clean:
	rm -f *.s

noop.c

void foo() {
  5 + 7;
}

twofuncs.c

void a(int i) {
}
void b() {
  a(5);
}

if.c

int max(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

loop.c

void print() {
  for (int i = 0; i < 5; ++i) {
  }
}

Haiku

Machine code, Latin.
Both unknown. But machine code…
All roads lead to it.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *