Skip to content

Latest commit

 

History

History
279 lines (236 loc) · 12.3 KB

c.org

File metadata and controls

279 lines (236 loc) · 12.3 KB

C

Warranty void if you fiddle with LD_PRELOAD or LD_LIBRARY_PATH.

Assembly language calls

Can be of the form [opcode][dest], [source]

Moves the value “1” into the EAX register

MOV EAX, 1

Can be of the form [opcode][operand]

  • jumps to and address in memory
  • in this case the hex address is 0xDEADBEEF
JMP 0xDEADBEEF

Stack-related mneumonic

  • PUSH [register]
  • POP [register]
  • CALL [function]
  • RET

Chip architecture and assembly language review

What is assembly

  • assembly = machine instructions
  • C is higher level language which gets translated into assembly by the compiler
key point
assembly language tells the computer exactly what to do and exactly HOW to do it
C or C++
will say “allocate memory” or “perform addition” x += 1;
Assembly
will say “put the byte from this address in memory into MOV EAX, x ADD EAX, 1 this register” or “jump to this location in memory”

Registers

definition
extremely high-performance memory located directly on the chip

General purpose registers

  • EAX, EBX, ECX, EDX
  • Used for performing operations on data

Special purpose registers

  • ESP: Stack pointer, points to the top of the stack. Manipulated by PUSH, POP, etc
  • EBP: Base pointer aka frame pointer.
  • ESI and EDI: ESI = source instruction, EDI = destination instruction.

Compile and run

echo '_Bool a;' | gcc -c -x c -
echo $?

Concurency

Debug

  • Compile flags
    -g3 -O0
        

Documentation

Examples

fprintf

fprintf(stderr, "CHECKPOINT REACHED @  %s:%i\n", __FILE__, __LINE__);

Create files

#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main ()
{
  int i;
  char file[1024];
  for (i = 0; i < 2048; ++i) {
    sprintf(file, "files/%d.txt", i);
    fopen (file, "w+");
  }
  sleep (60);
  return 0;
}

execve

#include <unistd.h>

int main(int argc, char *argv[])
{
  char file[] = "/home/oleg/.nix-profile/bin/nix-shell";
  char *const envp[] = { "HOME=/home/oleg", NULL };
  int result = execve(file, argv, envp);
  return result;
}

select

#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
  char buffer[128];
  int result, nread;
  fd_set inputs, testfds;
  struct timeval timeout;
  FD_ZERO(&inputs);
  FD_SET(0, &inputs);
  while(1) {
    testfds = inputs;
    timeout.tv_sec = 2;
    timeout.tv_usec = 500000;
    result = select(FD_SETSIZE, &testfds, (fd_set *)NULL,
                    (fd_set*)NULL, &timeout);
    switch(result)
      {
      case 0:
        printf("timeout\n");
        break;
      case -1:
        perror("select");
        exit(1);
      default:
        if (FD_ISSET(0, &testfds))
          {
            ioctl(0, FIONREAD, &nread);
            if (nread == 0) {
              printf("keyboard done\n");
              exit(0);
            }
            nread = read(0, buffer, nread);
            buffer[nread] = 0;
            printf("read %d from keyboard: %s\n", nread, buffer);
          }
        break;
      }
  }
}

Heap

  • large pool of operating system memory
  • used in dynamic memory allocation

Allocate memory on the heap

  • the new keyword in C++
  • the malloc in C
int * myArray = new int 10;

Languages

Learning

Libs

Programs

Proprietary

REPL

docker build -t bic https://github.com/hexagonal-sun/bic.git#master
docker run --rm -it bic:latest

Script

https://stackoverflow.com/questions/2482348/run-c-or-c-file-as-a-script

//usr/bin/env gcc -Wall -std=c99 -o $HOME/.cache/gcc/hello-world "$0" && exec $HOME/.cache/gcc/hello-world "$@"
#include <stdio.h>

int main ()
{
  printf ("Hello World\n");
  return 0;
}

http://drandom.blogspot.com/2013/12/shebang-for-c-programs.html

alcover/runc: compile and run C code

Alternative way use tcc

#!/usr/bin/tcc -run

rcrl

Stack

  • each process gets its own stack (assume single-threaded processes)
  • LIFO, like the data structure
  • contiguos block of memory (the process’s address space)

Stack frames

  • the stack consists of stack frames
  • contains the parameters to a function, its local variables, and the data necessary to recover the previous stack frame
  • when a function is called, a frame for that function is pushed onto the stack
  • when the function is done, we pop the stack frame and return to the caller
  • contains high-performance memory
  • usually fixed limits
to clarify
stack memory is high-performance in terms of ALLOCATION time, not ACCESS time
int myArray[10];

Tools

Tutorials

URLS