diff --git a/README.md b/README.md index 72f6c13..dac9c1e 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,58 @@ -# Assembly Language Programming Made Not So Scary +# A Gentle Introduction to Assembly Language Programming -This text book provides a fairly thorough examination of the ARM V8 ISA (Instruction Set Architecture). -It begins from the perspective of -a person knowledgeable in the C or C++ programming languages (or similar languages, of which there are many). -Early chapters bridge your knowledge of C or C++ backwards into assembly language driving home a very sharp -point: C is a high level assembly language. Further, assembly language is nothing to be scared about. +This textbook provides a gentle introduction to assembly language +programming. What makes this introduction "gentle" is that it assumes +the reader is already comfortable with C or C++ coding. We use this +assumed knowledge to **bridge** backward towards the low level ISA +(Instruction Set Architecture). + +We drive home a very sharp point: C **is** a high level assembly +language *and* assembly language is nothing to be scared about. ## For Whom Is This Book Intended? -As mentioned, if you are already familiar with C or any language descended from C, this book begins with what -you know. Later chapters dive deeply into the corners and recesses of the ARM V8 ISA and are suitable for -those wishing to master the rich instruction set of the 64 bit ARM processors. +As mentioned, if you are already familiar with C (or languages descended +from C), this book begins with what you already know. Later chapters +dive more deeply into the corners and recesses of the ARM V8 ISA and are +suitable for those wishing to master the rich instruction set of the 64 +bit ARM processors. ## Can This Book Be Used In Courses Covering Assembly Language? Yes, absolutely. -In fact, we would argue that the study of assembly language is extremely important to the -building of competent software engineers. Further, we would argue that teaching the x86 instruction set is sadistic and cruel as that ISA was born in the 1970s and has simply gotten more muddled with age. +In fact, we would argue that the study of assembly language is extremely +important to the building of competent software engineers. Further, we +would argue that teaching the x86 instruction set is cruel +as that ISA was born in the 1970s and has simply gotten more muddled +with age. -The MIPS instruction set is another ISA that is often covered in College level courses. While far kinder and gentler than the x86 ISA, the MIPS processor isn't nearly as relevant as the ARM family. Phones, tablets, laptops and even desktops contain ARM V8 processors making the study of +The MIPS instruction set is another ISA that is often covered in College +level courses. While kinder and gentler than the x86 ISA, the MIPS +processor isn't nearly as relevant as the ARM family. Phones, tablets, +laptops and even desktops contain ARM V8 processors making the study of the ARM ISA far more topical. ## Calling Convention Used In This Book -Assembly language programming is quite closely intertwined with both the underlying hardware architecture and the host -operating system. A "calling convention" refers to how functions are called and how parameters are passed. In -this book we will use the ARM LINUX conventions. This means: +Assembly language programming is quite closely intertwined with both the +underlying hardware architecture and the host operating system. A +"calling convention" refers to how functions are called and how +parameters are passed. In this book we will use the ARM LINUX +conventions. This means: -* You will need to run a ARM Linux VM on the Macintosh - even on ARM-based Macs. Why? Apple. That's why. -* You will need to run WSL (Windows Subsystem for Linux) on ARM-based Windows machines. +* You will need to run a ARM Linux VM on the Macintosh - even on + ARM-based Macs. Why? Apple. That's why. +* You will need to run WSL (Windows Subsystem for Linux) on ARM-based + Windows machines. * You will need to run an ARM Linux VM on x86-based Windows machines. ## A Lot of Names -As commendable as the ARM designs are, ARM's naming conventions for their Intellectual -Properties are horrid. In this book, AARCH64 and ARM V8 are taken to be synonyms for -the 64 bit ARM Instruction Set Architecture (ISA). +As commendable as the ARM designs are, ARM's naming conventions for +their Intellectual Properties are horrid. In this book, AARCH64 and ARM +V8 are taken to be synonyms for the 64 bit ARM Instruction Set +Architecture (ISA). ## Section 1 - Bridging from C / C++ to Assembly Language diff --git a/section_1/walkies/main.s b/section_1/walkies/main.s new file mode 100644 index 0000000..1628a0a --- /dev/null +++ b/section_1/walkies/main.s @@ -0,0 +1,122 @@ + .global main + .align 2 + +/* Walkies - a silly animation using four characters on the Linux + console. This program demonstrates low level IO (write()) and + is written in the form of an infinite loop (i.e. ^C or kill will + be required to halt the program). +*/ + +counter .req w20 +delta .req w21 + .equ MAX_COLUMN, 60 + .equ NUM_CHARS, 4 + + .section .rodata + +CHARS: .asciz "|/_\\" +TRM: .asciz " \r" + + .text + +main: stp x29, x30, [sp, -16]! + stp x20, x21, [sp, -16]! + + mov counter, wzr + mov w0, wzr + mov delta, 1 + +0: bl Pad + bl Emit + bl Terminator + add counter, counter, delta + mov w0, MAX_COLUMN + cmp w0, counter + bne 1f + neg delta, delta + b 2f +1: cbnz counter, 2f + neg delta, delta + +2: ldr x0, =usec + ldr w0, [x0] + bl usleep + b 0b + + ldp x20, x21, [sp], 16 + ldp x29, x30, [sp], 16 + mov w0, wzr + ret + +/* Terminator - outputs "\r " - notice the space after + the carriage return. +*/ + +Terminator: + stp x29, x30, [sp, -16]! + mov w0, 1 // 1 is stdout + ldr x1, =TRM // Pointer to string + mov w2, 2 // 2 characters being printed + bl write + ldp x29, x30, [sp], 16 + ret + +/* Emit - puts out the symbol derived from counter. +*/ + +Emit: stp x29, x30, [sp, -16]! + mov w0, counter + mov w1, NUM_CHARS + bl mod + ldr x1, =CHARS + add x1, x1, x0 + mov w0, 1 + mov w2, 1 + bl write + ldp x29, x30, [sp], 16 + ret + + +/* Pad - prints w0 spaces to the console. +*/ + +Pad: stp x29, x30, [sp, -16]! + str delta, [sp, -16]! + mov w21, wzr + +1: ldr x1, =buff + mov w2, ' ' + strb w2, [x1] // ' ' is pointed to by x1 + mov w0, 1 // 1 is stdout + mov w2, 1 // emitting 1 byte + bl write + add w21, w21, 1 + cmp w21, counter + blt 1b + + ldr delta, [sp], 16 + ldp x29, x30, [sp], 16 + ret + +/* mod(a, b) - implements a % b - AARCH64 lacks a mod instruction. + A strange place to economize, but there you have it. + + a comes to us in x0 + b comes to us in x1 + method: + * integer divide a by b + for example - 5 % 3 would need 5 // 3 yielding 1 + * multiply result by b + 1 * 3 is 3 + * subtract result from a + 5 - 3 is 2 and that's our return value. +*/ + +mod: sdiv x2, x0, x1 // x2 gets a // b + msub x0, x2, x1, x0 // x0 gets a - a // b * b + ret + + .data +buff: .space 4 +usec: .int 75000 +