Giter Site home page Giter Site logo

pkivolowitz / asm_book Goto Github PK

View Code? Open in Web Editor NEW
2.5K 2.5K 117.0 135.65 MB

A book teaching assembly language programming on the ARM 64 bit ISA. Along the way, good programming practices and insights into code development are offered which apply directly to higher level languages.

License: Other

C++ 8.63% C 7.51% Assembly 83.40% Shell 0.40% Python 0.06%
aarch64 arm64 assembly-language c cpp software-development

asm_book's Introduction

  • πŸ‘‹ Hi, I’m @pkivolowitz
  • πŸ‘€ I’m interested in code quality, CS education, OS and assembly language
  • 🌱 I’m currently learning how much fun it is to write a text book
  • πŸ’žοΈ I’m looking to consult on code reviews - I WILL find bugs!
  • πŸ“« How to reach me ...via github

I will be retiring to the Mobile Alabama area. I would welcome part-time consulting opportunities to perform code reviews within the many major industrial concerns in the area.

Please see Amazon for Get Off My L@wn, a zombie apocalypse novel where the main character flexes his leet coding skills to destroy zombie hordes.

asm_book's People

Contributors

alex-bender avatar jeffkilpatrick avatar pkivolowitz avatar rtobar avatar ss18 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

asm_book's Issues

While Loop Example README.md Possible Mistake

While Loop example README.md

In this portion of the code:

while (a >= b) {
    // CODE BLOCK
}

Here is the code for the while showing the addition of one new label
and one new unconditional branch:

    // Assume value of a is in x0                                       // 1 
    // Assume value of b is in x1                                       // 2 
                                                                        // 3 
 1: cmp     x0, x1                                                      // 4 
    bgt     2f                                                          // 5 
    // CODE BLOCK                                                       // 6 
    b       1b                                                          // 7 
                                                                        // 8 
2:                                                                      // 9 

The portion while (a >= b) as discovered in class may have imposed in error in the example.

Is this a typo?

Also, Perry received and Academy Award for Scientific and Technical Achievement for his invention of Shape Driven Warping and Morphing.

and -> an ?

Section 1 - Hello World - stp explanation

In the explanation of line 3 of Version 5 of the program. It says:

In a higher level language Line 3 would look like this:

*(--sp) = x21;
*(--sp) = x30;

That is, subtract 8 from the stack pointer and copy x21 to that location. Then, subtract 8 from the stack pointer and copy x30 to that location.

Please correct me if I'm wrong, but:
I think this explanation is misleading because it implies that after the instruction stp x21, x30, [sp, -16]! the stack looks like this:

+-----+
| ... |
+-----+
| x21 |
+-----+
| x30 | <-- sp
+-----+

When in reality the value of x21 is stored at the lower address and the value of x30 at the higher one.

Β 

On a site note: The same section contains a missing closing bracket

8 bytes (64 bits) wide. This accounts for the value 16 (i.e. 2 \* 8.

Could this be turned into an epub?

I was wondering if there were plans for an epub version. I’d like to read this on the train, but it’s only a 7” reader and pdfs are hard to read on it :-)

Pardon, assemble the book

Just like C is most higher level of assembly language, now it may will be appreciated if could get a higher level of documentation format - pdf.

Stack issue with SillyFunction

First, thanks for putting this out there. It's a very clear read and immensely useful.

It's possible I'm misunderstanding something, but should this line be ldr x30, [sp], 16 ?

Adding up the increments and decrements to stack in the entire program, it looks like it gets decremented by 16 three times, but incremented by 32 twice. Perhaps it's this line that needs changing. I'm unclear who is responsible for freeing stack consumed by parameters. Is it the caller or the callee?

As I say, it's possible I'm missing something entirely here and would appreciate a correction if that's the case.

Regards,

Why does this snippet print "Hello World" endlessly?

I have learned the section_1/hello_world and tested the code in a debian arm64 server.

This is the hello world snippet, hello_v6.s :

        .global _start
_start:
        str     x30, [sp, -16]!          // Preserve x30
        ldr     x0, =HW                 // Load address of string for puts
        bl      puts                        // puts(HW)
        ldr     x30, [sp], 16           // Restore x30
        mov     x0, xzr                 // return 0
        ret

        .data
HW:     .asciz  "Hello, World"
        .end

I compiled the code as follows:

as hello_v6.s -o hello_v6.o
ld hello_v6.o -lc

Then, run it:

./a.out

as --version:

GNU assembler (GNU Binutils for Debian) 2.35.2
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `aarch64-linux-gnu'.

ld --version:

GNU ld (GNU Binutils for Debian) 2.35.2
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.

It prints "Hello World" endlessly. I thought only one "Hello World" would be printed.

Would you like to tell me why?

How to assembly section1/hello_world/v6.S?

Thanks for your work so far.

I have ./section_1/hello_world/v6.S

#include "apple-linux-convergence.S"

        .text
        .p2align    2
        GLABEL      main

MAIN
        PUSH_P      x21, x30
        PUSH_R      x29
        mov         x29, sp

        mov         x21, x1

1:      ldr         x0, [x21], 8
        cbz         x0, 2f
        CRT         puts
        b           1b

2:      POP_R       x29
        POP_P       x21, x30
        mov         x0, xzr
        ret

        .end

I also have ./section_1/hello_world/apple-linux-convergence.S

/*  Macros to permit the "same" assembly language to build on ARM64
    Linux systems as well as Apple Silicon systems.

    See the fuller documentation at:
    https://github.com/pkivolowitz/asm_book/blob/main/macros/README.md

    Perry Kivolowitz
    A Gentle Introduction to Assembly Language
*/

.macro  GLD_PTR     xreg, label
#if defined(__APPLE__)
        adrp	    \xreg, _\label@GOTPAGE
        ldr	        \xreg, [\xreg, _\label@GOTPAGEOFF]
#else
        ldr         \xreg, =\label
        ldr         \xreg, [\xreg]
#endif
.endm

.macro  GLD_ADDR    xreg, label     // Get a global address
#if defined(__APPLE__)
        adrp	    \xreg, _\label@GOTPAGE
        add         \xreg, \xreg, _\label@GOTPAGEOFF
#else
        ldr         \xreg, =\label
#endif
.endm

.macro  LLD_ADDR xreg, label
#if defined(__APPLE__)
        adrp    \xreg, \label@PAGE
        add     \xreg, \xreg, \label@PAGEOFF
#else
        ldr         \xreg, =\label
#endif
.endm

.macro  LLD_DBL xreg, dreg, label
#if defined(__APPLE__)
        adrp    \xreg, \label@PAGE
        add     \xreg, \xreg, \label@PAGEOFF
        ldur    \dreg, [\xreg]
//      fmov    \dreg, \xreg
#else
        ldr     \xreg, =\label
        ldur    \dreg, [\xreg]
#endif
.endm

.macro  LLD_FLT xreg, sreg, label
#if defined(__APPLE__)
        adrp    \xreg, \label@PAGE
        add     \xreg, \xreg, \label@PAGEOFF
        ldur    \sreg, [\xreg]
#else
        ldr     \xreg, =\label
        ldur    \sreg, [\xreg]
#endif
.endm

.macro GLABEL label
#if defined(__APPLE__)
        .global _\label
#else
        .global \label
#endif
.endm

.macro MAIN
#if defined(__APPLE__)
_main:
#else
main:
#endif
.endm

/*  Fetching the address of the externally defined errno is quite
    different on Apple and Linux. This macro leaves the address of
    errno in x0.
*/
.macro  ERRNO_ADDR
#if defined(__APPLE__)
        bl      ___error
#else
        bl      __errno_location
#endif
.endm

.macro  CRT label
#if defined(__APPLE__)
        bl  _\label
#else
        bl  \label
#endif
.endm

.macro  START_PROC          // after starting label
        .cfi_startproc
.endm

.macro  END_PROC            // after the return
        .cfi_endproc
.endm

.macro  PUSH_P  a, b 
        stp     \a, \b, [sp, -16]!
.endm

.macro  PUSH_R  a 
        str     \a, [sp, -16]!
.endm

.macro  POP_P   a, b 
        ldp     \a, \b, [sp], 16
.endm

.macro  POP_R   a 
        ldr     \a, [sp], 16
.endm

/*  The smaller of src_a and src_b is put into dest. A cmp instruction
    or other instruction that sets the flags must be performed first.
    This macro makes it easy to remember which register does what in the
    csel.
    
    Thank you to u/TNorthover for nudge to add the cmp.
*/

.macro  MIN     src_a, src_b, dest
        cmp     \src_a, \src_b
        csel    \dest, \src_a, \src_b, LT
.endm

/*  The larger of src_a and src_b is put into dest. A cmp instruction
    or other instruction that sets the flags must be performed first.
    This macro makes it easy to remember which register does what in the
    csel.

    Thank you to u/TNorthover for nudge to add the cmp.
*/

.macro  MAX     src_a, src_b, dest
        cmp     \src_a, \src_b
        csel    \dest, \src_a, \src_b, GT
.endm

.macro  AASCIZ      label, string
        .p2align    2
\label: .asciz      "\string"
.endm

.macro  MOD         src_a, src_b, dest, scratch
        sdiv        \scratch, \src_a, \src_b
        msub        \dest, \scratch, \src_b, \src_a
.endm

However I cant assemble it.

as -o v6.o v6.S

When I run that command I get this error.

$ as -o v6.o v6.S
v6.S:5:9: error: unrecognized instruction mnemonic
        GLABEL      main
        ^
v6.S:7:1: error: unrecognized instruction mnemonic, did you mean: fmin, smin, umin?
MAIN
^
v6.S:8:9: error: unrecognized instruction mnemonic
        PUSH_P      x21, x30
        ^
v6.S:9:9: error: unrecognized instruction mnemonic, did you mean: ushr?
        PUSH_R      x29
        ^
v6.S:16:9: error: unrecognized instruction mnemonic, did you mean: cnt, ret?
        CRT         puts
        ^
v6.S:19:9: error: unrecognized instruction mnemonic
2:      POP_R       x29
        ^
v6.S:20:9: error: unrecognized instruction mnemonic
        POP_P       x21, x30

Could you please advise? Thanks in advance for the support.

section 1 - alignment - possible typo?

I am very interested in this topic. I read I can get points to spot typos in the book? :) Possibly the little endianness part of the chapter contains a typo. I get different results/.
Here is a link to my gist with explanation

<script src="https://gist.github.com/multitudes/da102c7cec4416c6854769cdba141828.js"></script>

The correct representation of the actual contents for little endian encoding should be:

Type Logical Contents ....... Actual Contents
long aabbccddeeff0011 -> 1100 eeff ccdd aabb

"note the overlap"

I'm guessing this is still a WIP, but it's a little confusing in the float registers that you're asked to ".. note the overlap of two single precision floats within a single double precision floating point register.", but there is apparently only one single precision register indicated. (S0).

I may be being a little pedantic here, but I wanted to note the potential for confusion.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.