News for hackers. Stuff that matters.
Re:A fundamental introduction to x86 assembly programming by shermanyo on Sunday May 15, 2016 @11:07PM (#11703968)
This book was what made C click for me (in the few chapters I digested way back when). I actually stopped reading twice because I suddenly understood something that had blocked my progress in C, and went on my way for a year or two until I decided to pick the book up again.

For a quick idea of what ASM can look like if you build up the foundations step-by-step, and understand what you're working with:

    .include "record-def.s"
    .include "linux.s"
    #PURPOSE: This function reads a record from the file descriptor
    #
    #INPUT: The file descriptor and a buffer
    #
    #OUTPUT: This function writes the data to the buffer
    # and returns a status code.
    #
    #STACK LOCAL VARIABLES
    .equ ST_READ_BUFFER, 8
    .equ ST_FILEDES, 12
    .section .text
    .globl read_record
    .type read_record, @function
    read_record:
    pushl %ebp
    movl %esp, %ebp
    pushl %ebx
    movl ST_FILEDES(%ebp), %ebx
    movl ST_READ_BUFFER(%ebp), %ecx
    movl $RECORD_SIZE, %edx
    movl $SYS_READ, %eax
    int $LINUX_SYSCALL
    #NOTE - %eax has the return value, which we will give back to our calling program
    popl %ebx
    movl %ebp, %esp
    popl %ebp
    ret
  
With the definitions in place, its like a whole different language.

For reference, the definitions are simply a text file with contents similar to:

    #System Call Numbers
    .equ SYS_EXIT, 1
    .equ SYS_READ, 3
    .equ SYS_WRITE, 4
    .equ SYS_OPEN, 5
    .equ SYS_CLOSE, 6
    .equ SYS_BRK, 45
  
Or (record-defs.s in the example) clearly describing the data with:

    .equ RECORD_FIRSTNAME, 0
    .equ RECORD_LASTNAME, 40
    .equ RECORD_ADDRESS, 80
    .equ RECORD_AGE, 320
    .equ RECORD_SIZE, 324
  
This book opened my eyes to the concrete, data driven nature of the problems I'm trying to solve, at an atomic level. It somehow dispelled all the magic behind programming, while exciting the mechanical side of my brain, leaving me with that "its just a machine, I can solve any problem if I just trace things patiently until I understand the parts and how they interact".
Re:A fundamental introduction to x86 assembly programming by d0mine on Friday May 20, 2016 @08:58PM (#11742639)
Unrelated: the code is equivalent to read(fd, buf, RECORD_SIZE) that may read less than RECORD_SIZE.

The function should read in a loop, to fulfill its contract (read a record).

  #include <unistd.h>
  
  ssize_t read_record(int fd, void* buf, size_t record_size, size_t* written) {
    for (*written = 0; *written < record_size; ) {
      ssize_t n = read(fd, (char*)buf + *written, record_size - *written);
      if (n < 1)
        return n; /* error or EOF */
      *written += n;
    }
    return *written;
  }