flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Assembly language textbook(s)

Author
Thread Post new topic Reply to topic
Hotwire



Joined: 17 Sep 2015
Posts: 18
Hotwire 06 Jul 2022, 08:57
Hello there

I wonder if someone can advise nice Assembly language textbooks. Most of the books I've seen so far are about moving operands and some very basic arithmetical instruction while great amount of cpu instructions are abandoned.

I will be very grateful is someone can provide titles of the books or links to the tutorials with pure assembly art.
Post 06 Jul 2022, 08:57
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4162
Location: vpcmpistri
bitRAKE 06 Jul 2022, 15:31
Art is very subjective, but I find size coding to require thinking about the instruction set differently than optimization (speed) coding. And in that regard I recently stubbled across Oscar Toledo's work. Reading the source code, I noticed he has a couple books on boot sector games programming. This might not be what you are looking for?

I've owned a number of assembly books:

Mastering Turbo Assembler by Tom Swan
(good copies are very expensive)

All Michael Abrash's Graphics Programming books
Black Book is on github

Of course, the Intel Manuals can be purchased in print form.

I've not bought anything recent though. Are you interested in modern coding, like 64-bit stuff, optimization, etc. Do you have an OS preference? At the other end of the spectrum I would recommend art such as http://0x80.pl/articles/

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 06 Jul 2022, 15:31
View user's profile Send private message Visit poster's website Reply with quote
FlierMate1



Joined: 31 May 2022
Posts: 118
FlierMate1 07 Jul 2022, 10:55
Hotwire wrote:

I will be very grateful is someone can provide.... links to the tutorials with pure assembly art.


bitRAKE wrote:
size coding


I share an "Intro to Assembly Optimization" by a security researcher (malware analyst).

It has few different source files on his GitHub:
https://github.com/netspooky/i2ao/tree/master/src

hello.c --> bigsmile.asm --> smile.asm --> tiny.asm / smol.asm

From C to Assembly. The deoptimized Assembly uses "mov rdi,0" and optimized Assembly uses "xor rdi,rdi" and several other tricks.

It makes the final executable file smaller and smaller.
Post 07 Jul 2022, 10:55
View user's profile Send private message Reply with quote
Hotwire



Joined: 17 Sep 2015
Posts: 18
Hotwire 08 Jul 2022, 11:07
bitRAKE, FlierMate1
Many thanks

Working on a new problem I've recently realized that my assembly code is a simple translation from High Level Language to assembly (pretty much straightforward). I know several tricks but amount of these tricks is poor.
Post 08 Jul 2022, 11:07
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4162
Location: vpcmpistri
bitRAKE 09 Jul 2022, 00:13
Challenge yourself by making your translation fewer lines of code than the HLL code.

For example, this Pascal code:
Code:
   function IsVictory(Field: Field): Boolean;
   var
      Row, Col: Integer;
   begin
      with Field do
         for Row := 0 to Rows-1 do
            for Col := 0 to Cols-1 do
               case States[Row][Col] of
                  Open:    if Cells[Row][Col] <> Empty then Exit(False);
                  Closed:  Exit(False);
                  Flagged: if Cells[Row][Col] <> Bomb then Exit(False);
               end;
      IsVictory := True;
   end;    
... I've translated by changing the data model and using the carry flag for return.
(Hint: all data elements are byte size.)
Code:
IsVictory:
        movzx eax,[rsi+FIELD.cols] ; to clear high word
        mul [rsi+FIELD.rows]
        push rsi
        xchg ecx,eax
  .keep_scanning:
        lodsb
        test al,1000_0001b      ; clears carry flag
        jpe .false
        loop .keep_scanning
        stc
  .false:
        pop rsi
        retn    
... obviously, it is much fewer bytes of code as well.

Edit: link to larger work, to provide greater context.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup


Last edited by bitRAKE on 09 Jul 2022, 15:05; edited 1 time in total
Post 09 Jul 2022, 00:13
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 09 Jul 2022, 09:15
bitRAKE wrote:
Code:
IsVictory:
        movzx eax,[rsi+FIELD.cols] ; to clear high word
        mul [rsi+FIELD.rows]
        push rsi
        xchg ecx,eax
  .keep_scanning:
        lodsb
        test al,1000_0001b      ; clears carry flag
        jpe .false
        loop .keep_scanning
        stc
  .false:
        pop rsi
        retn    
... obviously, it is much fewer bytes of code as well.

Wow, finally a good example of PF usage. Thanks for sharing.
Post 09 Jul 2022, 09:15
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1878
Roman 09 Jul 2022, 12:13
Quote:

Wow, finally a good example of PF usage. Thanks for sharing.

what is PF ?
Post 09 Jul 2022, 12:13
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1066
Location: Russia
macomics 09 Jul 2022, 13:28
efl.pf = 4 (parity flag, bit 2)
Post 09 Jul 2022, 13:28
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4162
Location: vpcmpistri
bitRAKE 09 Jul 2022, 14:56
DimonSoft wrote:
Wow, finally a good example of PF usage. Thanks for sharing.
The general method used here should be known. We are talking about the sum of bits creating groups of solutions.

For the three bit case we have: 000, 011, 101, 110 ...even parity.

Any time we can map our data model to one of these groups (or the negation), work can be eliminated.

I've also seen it used to swap bits:
Code:
    test al,0100_0010b
    jpe @F
    xor al,0100_0010b
@@:    


[Because the flag is restricted to the low byte result.]
Parity of larger sized values is bit zero of the population count. If we are doing the population count, maybe the quadrity is useful - the least two bits of count. etc. (Still, I think about it in terms of partitioning the solution space.)
Code:
00000 ; quadrity= 0
01111
10111
11011
11101
11110

00001 ; quadrity= 1
00010
00100
01000
10000
11111

00011 ; quadrity= 2
00101
00110
01001
01010
01100
10001
10010
10100
11000

00111 ; quadrity= 3
01011
01101
01110
10011
10101
10110
11001
11010
11100    
I haven't said modular arithmetic. Yet, some here already see it, and higher order n-tuples. We are just masking the count of bits though - two instructions.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 09 Jul 2022, 14:56
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4162
Location: vpcmpistri
bitRAKE 10 Jul 2022, 01:58
Let us look at another thing - completely different and also the same: Examing the Pascal, we see a number of functions acting on the neighborhood of a cell. Each of these functions rely on a bounds checking function.

We can consolidate all these into a single general neighbor count function, but first we should resolve all these loops into a single loop. This is a verbose, data-centric view of the problem:
Code:
iterate <_dx,_dy>,              \
        -1,1,   0,1,    1,1,    \
        -1,0,           1,0,    \
        -1,-1,  0,-1,   1,-1

        ; do something

end iterate    
... here we recognize the range of numbers in this two dimensional array: it is sufficient to use 2-bit signed numbers. These 2-bit signed numbers compress (there is a cycle where adjacent bit pairs are the same) temporally into 16-bits. At first, I wanted to code something like:
Code:
        mov ax,bx
        rol bx,2
        sar al,6
        sar ah,6    
... look how greedy I am! But alas this is not possible. Sad I must settle on something larger. Yet, we can still see how the entire two dimensional array fits within a register. Also, note how the value is non-symmetric -- making our exit condition unique -- it is also our loop counter!

Edit: Just simple change solved the problem:
Code:
        mov ax,bx
        rol bx,2 ; left or right shifts, 2 mod 4 should work: ror bx,6 perhaps
        sar ax,6
        sar al,6    
... there are 96 solutions for BX.
So, it might take on other uses.
Post 10 Jul 2022, 01:58
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4162
Location: vpcmpistri
bitRAKE 10 Jul 2022, 15:25
Compilers often get confused by signed/unsigned numbers and the conversion between them. (Or rather, the syntactic sugar required to make them do what you want is enough to rot your teeth out!) If we stay with signed numbers the bounds checking needs both min and max test for each parameter. But we know more than the compiler and can use unsigned numbers for the bounds check - only a single check/branch for each parameter.

This is possible because the change in value is only ever +/-1; meaning 0xFF is the only underflow possible which is also the maximum unsigned value (i.e. same as an overflow). This limits our range to 0xFF (i.e. [0,0xFE]) which is not a problem in most cases.
Code:
macro bound_parameter parm*,delta*,range*,under_over_flow*
        add parm,delta          ; signed
        cmp parm,range          ; unsigned
        jnc under_over_flow     ; unsigned
end macro    
Obviously, extends to any data size, SIMD.

More generally, we can say: if the underflow does not land within the range, then a single unsigned check is possible.
Post 10 Jul 2022, 15:25
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 13180
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 10 Jul 2022, 16:44
Hotwire wrote:
Hello there

I wonder if someone can advise nice Assembly language textbooks. Most of the books I've seen so far are about moving operands and some very basic arithmetical instruction while great amount of cpu instructions are abandoned.

I will be very grateful is someone can provide titles of the books or links to the tutorials with pure assembly art.


the best vault on earth
https://www.pdfdrive.com/assembly-language-books.html
Post 10 Jul 2022, 16:44
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.