├── .gitattributes ├── 9781484274361.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md ├── arithmetic.s ├── binaryexit.s ├── errata.md ├── exponent.s ├── exponentloop.s ├── followthejump.s ├── infiniteloop.s ├── jmpexample.s ├── myexit.s └── valuesize.s /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484274361.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/learn-to-program-w-assembly/8ba21ed0f11a540ac2bb7e07902d1056e39ee0fe/9781484274361.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2021 Jonathan Bartlett 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Learn to Program with Assembly*](https://link.springer.com/book/10.1007/978-1-4842-7437-8) by Jonathan Bartlett (Apress, 2021). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484274361.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /arithmetic.s: -------------------------------------------------------------------------------- 1 | .globl _start 2 | .section .text 3 | _start: 4 | # Perform various arithemtic functions 5 | movq $3, %rdi 6 | movq %rdi, %rax 7 | addq %rdi, %rax 8 | mulq %rdi 9 | movq $2, %rdi 10 | addq %rdi, %rax 11 | movq $4, %rdi 12 | mulq %rdi 13 | movq %rax, %rdi 14 | 15 | # Set the exit system call number 16 | movq $60, %rax 17 | 18 | # Perform the system call 19 | syscall 20 | -------------------------------------------------------------------------------- /binaryexit.s: -------------------------------------------------------------------------------- 1 | .section .text 2 | .globl _start 3 | _start: 4 | movq $0b1101, %rdi 5 | movq $60, %rax 6 | syscall 7 | -------------------------------------------------------------------------------- /errata.md: -------------------------------------------------------------------------------- 1 | # Errata for *Learn to Program with Assembly* 2 | 3 | On **page xx** [Summary of error]: 4 | 5 | Details of error here. Highlight key pieces in **bold**. 6 | 7 | *** 8 | 9 | On **page xx** [Summary of error]: 10 | 11 | Details of error here. Highlight key pieces in **bold**. 12 | 13 | *** -------------------------------------------------------------------------------- /exponent.s: -------------------------------------------------------------------------------- 1 | .globl _start 2 | 3 | # This will calculate 2^3. 4 | # You can modify %rbx and %rcx to calculate 5 | # another exponential. 6 | 7 | .section .text 8 | _start: 9 | # %rbx will hold the base 10 | movq $2, %rbx 11 | 12 | # %rcx will hold the current exponent count 13 | movq $3, %rcx 14 | 15 | # Store the accumulated value in %rax 16 | movq $1, %rax 17 | 18 | mainloop: 19 | # Adding zero will allow us to use the flags to 20 | # determine if %rcx has zero to begin with 21 | addq $0, %rcx 22 | 23 | # If the exponent is zero, we are done 24 | jz complete 25 | 26 | # Otherwise, multiply the accumulated value by our base 27 | mulq %rbx 28 | 29 | # Decrease the counter 30 | decq %rcx 31 | 32 | # Go back to the beginning of the loop and try again 33 | jmp mainloop 34 | 35 | complete: 36 | # Move the accumulated value to %rdi so we can return it 37 | movq %rax, %rdi 38 | # call the "exit" system call 39 | movq $60, %rax 40 | syscall 41 | -------------------------------------------------------------------------------- /exponentloop.s: -------------------------------------------------------------------------------- 1 | .globl _start 2 | 3 | # This will calculate 2^3. 4 | # You can modify %rbx and %rcx to calculate 5 | # another exponential. 6 | 7 | .section .text 8 | _start: 9 | # %rbx will hold the base 10 | movq $2, %rbx 11 | 12 | # %rcx will hold the current exponent count 13 | movq $3, %rcx 14 | 15 | # Store the accumulated value in rax 16 | movq $1, %rax 17 | 18 | # If the exponent is equal to zero, we are done 19 | cmpq $0, %rcx 20 | je complete 21 | 22 | mainloop: 23 | # Multiply the accumulated value by our base 24 | mulq %rbx 25 | 26 | # Decrement %rcx, go back to loop label if %rcx is 27 | # not yet zero 28 | loopq mainloop 29 | 30 | complete: 31 | # Move the accumulated value to %rdi so we can return it 32 | movq %rax, %rdi 33 | # call the "exit" system call 34 | movq $60, %rax 35 | syscall 36 | -------------------------------------------------------------------------------- /followthejump.s: -------------------------------------------------------------------------------- 1 | .globl _start 2 | 3 | .section .text 4 | _start: 5 | movq $25, %rax 6 | jmp thelabel 7 | 8 | somewhere: 9 | movq %rax, %rdi 10 | jmp anotherlabel 11 | 12 | label1: 13 | addq %rbx, %rax 14 | movq $5, %rbx 15 | jmp here 16 | 17 | labellabel: 18 | syscall 19 | 20 | anotherlabel: 21 | movq $60, %rax 22 | jmp labellabel 23 | 24 | thelabel: 25 | movq %rax, %rbx 26 | jmp there 27 | 28 | 29 | here: 30 | divq %rbx 31 | jmp somewhere 32 | 33 | there: 34 | addq $5, %rbx 35 | jmp label1 36 | 37 | anywhere: 38 | jmp thelabel 39 | -------------------------------------------------------------------------------- /infiniteloop.s: -------------------------------------------------------------------------------- 1 | .globl _start 2 | 3 | .section .text 4 | _start: 5 | movq $60, %rax 6 | 7 | another_location: 8 | movq $8, %rdi 9 | 10 | jmp another_location 11 | 12 | # This never gets executed 13 | syscall 14 | -------------------------------------------------------------------------------- /jmpexample.s: -------------------------------------------------------------------------------- 1 | .globl _start 2 | 3 | .section .text 4 | _start: 5 | movq $7, %rdi 6 | jmp nextplace 7 | 8 | # These two instructions are skipped 9 | movq $8, %rbx 10 | addq %rbx, %rdi 11 | 12 | nextplace: 13 | movq $60, %rax 14 | syscall 15 | -------------------------------------------------------------------------------- /myexit.s: -------------------------------------------------------------------------------- 1 | # My first program. This is a comment. 2 | 3 | .globl _start 4 | 5 | .section .text 6 | 7 | _start: 8 | movq $60, %rax 9 | movq $3, %rdi 10 | syscall 11 | -------------------------------------------------------------------------------- /valuesize.s: -------------------------------------------------------------------------------- 1 | .section .text 2 | .globl _start 3 | _start: 4 | movw $0b0000000100000010, %bx 5 | addb %bh, %bl 6 | movb $0, %bh 7 | 8 | movq %rbx, %rdi 9 | 10 | movq $60, %rax 11 | syscall 12 | --------------------------------------------------------------------------------