Author Topic: Book : alert : The Art of 64-Bit Assembly  (Read 3891 times)

mayuresh

  • Regular Member
  • *
  • Posts: 4
Book : alert : The Art of 64-Bit Assembly
« on: July 11, 2021, 10:13:49 PM »
Randall Hyde is releasing a new edition of his book "The Art of 64-Bit Assembly" in October 2021 and it uses MASM.
Further information at https://nostarch.com/art-64-bit-assembly.
Have fun.

PS: I had posted this in the MASM64 section. Sorry to post here again.

LiaoMi

  • Member
  • *****
  • Posts: 1032
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #1 on: July 11, 2021, 10:39:09 PM »


The Art of 64-Bit Assembly
x86-64 Machine Organization and Programming by Randall Hyde
October 2021, 1032 pp.
ISBN-13:
9781718501089

Chapter6: Arithmetic - Example
https://nostarch.com/download/samples/TheArtof64BitAssemblyLanguage_ch6.pdf

Table of contents
Part I: Machine Organization
Chapter 1: Hello, World of Assembly Language
Chapter 2: Computer Data Representation and Operations
Chapter 3: Memory Access and Organization
Chapter 4: Constants, Variables, and Data Types
Part II: Assembly Language Programming
Chapter5: Procedures
Chapter6: Arithmetic
Chapter 7: Low-Level Control Structures
Chapter 8: Advanced Arithmetic
Chapter 9: Numeric Conversion
Chapter 10: Table Lookups

Chapter 11: SIMD Instructions
Chapter 12: Bit Manipulation
Chapter 13: Macros and the MASM Compile-Time Language
Chapter 14: The String Instructions
Chapter 15: Managing Complex Projects
Chapter 16: Standalone Assembly Language Programs
Part III: Reference Material
Appendix A: ASCII Character Set
Appendix B: Glossary
Appendix C: Installing and Using Visual Studio Appendix D: The Windows Command-Line Interpreter Appendix E: Answers to Questions

The chapters in red are included in this Early Access PDF.

jj2007

  • Member
  • *****
  • Posts: 13020
  • Assembler is fun ;-)
    • MasmBasic
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #2 on: July 11, 2021, 11:30:02 PM »
Chapter6: Arithmetic - Example
https://nostarch.com/download/samples/TheArtof64BitAssemblyLanguage_ch6.pdf

Well written, but I count 47 occurrences of rax vs 187 of eax. Not exactly a complete rewrite...

daydreamer

  • Member
  • *****
  • Posts: 2095
  • beer glass
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #3 on: July 11, 2021, 11:49:51 PM »
curious on his SIMD chapter,liked much his former AoA,also liked his MMX chapters
I made 64bit versions of his previous example programs as exercise,2^x,fibonacci,create chessboard pattern


SIMD fan and macro fan
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."

ttribelli

  • Regular Member
  • *
  • Posts: 7
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #4 on: December 31, 2021, 01:25:02 AM »
Well written, but I count 47 occurrences of rax vs 187 of eax. Not exactly a complete rewrite...

32-bit registers are sometimes the better choice even in 64-bit mode. I'm going to bet that a lot of those EAX uses were in things like xor eax, eax. Look at the generated code for EAX vs RAX.

Code: [Select]
33 C0                            xor         eax, eax
48/ 33 C0                        xor         rax, rax

Even in 64-bit mode the default register size is 32-bit. So to access a 64-bit register you need the extra prefix byte that you see above.

Also in 64-bit mode various operations on 32-bit registers zero extend to 64-bit. So the two instructions above do that same thing, both zero 64-bit RAX.

jj2007

  • Member
  • *****
  • Posts: 13020
  • Assembler is fun ;-)
    • MasmBasic
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #5 on: December 31, 2021, 04:19:24 AM »
32-bit registers are sometimes the better choice even in 64-bit mode.

That's correct. My observation was somewhat superficial ;-)

Welcome to the Forum :thup:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9806
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #6 on: December 31, 2021, 08:50:39 AM »
I am not a fan of mixed more code design, its either 32 or 64 bit, not both. There are times in 64 bit code where you need a 32 bit register but the popular mixed more code you see in compiler code can be highly error prone when coded manually. Much of Win64 API code is still 32 bit under the hood and you must know how to work with it.

I have over time kept hearing the code size argument but that in itself is a left over from MS-DOS coding where it mattered. CPU instruction scheduling tends to work at the instruction level at close range so the different between,

xor eax, eax
    and
xor rax, rax

in terms of CPU instruction scheduling is that both are simply one instruction.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

daydreamer

  • Member
  • *****
  • Posts: 2095
  • beer glass
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #7 on: December 31, 2021, 05:33:19 PM »
but writing 64 bit example code to learn newbies 64 bit best to stick to 64bit
And they can learn size/speed optimisation later
SIMD fan and macro fan
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."

ttribelli

  • Regular Member
  • *
  • Posts: 7
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #8 on: January 13, 2022, 09:00:40 PM »

xor eax, eax
    and
xor rax, rax

in terms of CPU instruction scheduling is that both are simply one instruction.

Sure, x86 is a facade and for all I know both may be translated to the same micro-ops for scheduling and execution. However there is still the issue that the 64-bit incarnation requires an extra byte. That's adding to the number of bytes that must be fetched from RAM, reducing the number of instructions that can fit into a cache line, etc.

ttribelli

  • Regular Member
  • *
  • Posts: 7
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #9 on: January 13, 2022, 09:19:03 PM »
but writing 64 bit example code to learn newbies 64 bit best to stick to 64bit
And they can learn size/speed optimisation later

I definitely understand that sentiment, using 32-bit instructions felt wrong to me when I started learning 64-bit. However we need to learn that 32-bit instructions in 64-bit mode behave differently than in 32-bit mode. In the past we were free to manipulate half a register, say AX in 32-bit mode, and the other half, EAX:31..16, would remain unchanged. That's no longer true and being unaware could lead to bugs.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9806
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #10 on: January 13, 2022, 09:44:23 PM »
> reducing the number of instructions that can fit into a cache line

Herein lies the problem, in the real mode DOS era when you were running very old hardware(.386 etc ...) keeping code length to a minimum mattered at times but we don't live in the i386 era and hardware is some powers more capable. Multiple pipelines introduced just after a .486dx also changed how code is prefetched.

Come to the modern hardware era with high core counts and large amounts of memory along with a far wider range of instructions including prefetch instructions and instructions that load and save bypassing the load cache and the rules have changed a lot. If you went out of your way to write long dependencies, you could get effects related to a cache line length but there is a perfectly good response to bad design, its called the clock.

If you can manage to get instruction stalls while some are waiting for others to be retired, then you rewrite it so it no longer happens.

As far as differences in 64 bit, remember that you have far more integer registers and you don't suffer the bottlenecks of 32 bit, its a design change that comes with practice but 64 bit can do so much more.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 13020
  • Assembler is fun ;-)
    • MasmBasic
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #11 on: January 13, 2022, 09:49:08 PM »
In the past we were free to manipulate half a register, say AX in 32-bit mode, and the other half, EAX:31..16, would remain unchanged. That's no longer true

Check your assumptions.

Code: [Select]
include \Masm32\MasmBasic\Res\JBasic.inc ; ## console demo, builds in 32- or 64-bit mode with UAsm, ML, AsmC ##
usedeb=1 ; 1=use the deb macro
Init ; OPT_64 1 ; put 0 for 32 bit, 1 for 64 bit assembly
  PrintLine Chr$("This program was assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format.")
  mov rax, 1234567890123456h
  deb 4, "init", x:eax
  mov al, 11h
  deb 4, "mov al", x:eax
  mov ax, 2211h
  deb 4, "mov ax", x:eax
  mov eax, 44332211h
  deb 4, "mov eax", x:eax
EndOfCode

Code: [Select]
This program was assembled with ml64 in 64-bit format.
init    x:eax   1234567890123456h
mov al  x:eax   1234567890123411h
mov ax  x:eax   1234567890122211h
mov eax x:eax   44332211h

Biterider

  • Member
  • ****
  • Posts: 984
  • ObjAsm Developer
    • ObjAsm
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #12 on: January 13, 2022, 11:27:45 PM »
Hi JJ
Perhaps he's referring to 64-bit registers and using the 32-bit registers as an analogy.
Code: [Select]
mov rax, -1    ;0FFFFFFFFFFFFFFFFh
mov eax, 0     ;0000000000000000h

Biterider

jj2007

  • Member
  • *****
  • Posts: 13020
  • Assembler is fun ;-)
    • MasmBasic
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #13 on: January 14, 2022, 01:59:02 AM »
Better?

Code: [Select]
  mov rax, 1234567890123456h
  deb 4, "init", x:rax
  mov al, 11h
  deb 4, "mov al", x:rax
  mov ax, 2211h
  deb 4, "mov ax", x:rax
  mov eax, 44332211h
  deb 4, "mov eax", x:rax

Code: [Select]
init    x:rax   1234567890123456h
mov al  x:rax   1234567890123411h
mov ax  x:rax   1234567890122211h
mov eax x:rax   44332211h

HSE

  • Member
  • *****
  • Posts: 2197
  • AMD 7-32 / i3 10-64
Re: Book : alert : The Art of 64-Bit Assembly
« Reply #14 on: January 14, 2022, 05:42:32 AM »
Hi JJ!

I was forgetting more interesting part  :biggrin:

Code: [Select]
   mov rax, 1234567890123456h
    freg_push rax
    conout "init    = ", hex$(rax), lf

    freg_peek rax
    mov al, 11h
    conout "mov al  = ", hex$(rax), lf

    freg_peek rax
    mov ax, 2211h
    conout "mov ax  = ", hex$(rax), lf

    freg_pop rax
    mov eax, 44332211h
    conout "mov eax = ", hex$(rax), lf, lf
 
Code: [Select]
init    = 1234567890123456
mov al  = 1234567890123411
mov ax  = 1234567890122211
mov eax = 44332211

Press any key to continue...
Equations in Assembly: SmplMath