hn-classics/_stories/2007/10437070.md

208 lines
8.5 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
created_at: '2015-10-23T06:12:27.000Z'
title: How NOP Nearly Became a Non-NOP on AMD64 (2007)
url: http://www.pagetable.com/?p=6
author: mtviewdave
points: 72
story_text:
comment_text:
num_comments: 16
story_id:
story_title:
story_url:
parent_id:
created_at_i: 1445580747
_tags:
- story
- author_mtviewdave
- story_10437070
objectID: '10437070'
year: 2007
---
[Source](http://www.pagetable.com/?p=6 "Permalink to How NOP nearly became a non-NOP on AMD64 | pagetable.com")
# How NOP nearly became a non-NOP on AMD64 | pagetable.com
# [pagetable.com][1]
## Some Assembly Required
### Menu
[Skip to content][2]
* [Home][3]
* [About][4]
# How NOP nearly became a non-NOP on AMD64
[4 Replies][5]
AMD64 is a quite clean extension of the i386 instruction set, it obsoletes many rarely used features of the i386 and introduces new registers, making the instruction set a lot more… logical.
But there is one feature, actually a nice trick of the 8086/8088, which would make problems with the AMD64 extensions:
The NOP instruction would now clear the upper 32 bits of EAX in 64 bit mode.
How can this be? Just like many RISC CPUs, the 8086 did not have a real NOP instruction. An assembler would translate the mnemonic “nop” into “xchg ax, ax” (opcode 0×90)- this instruction has no effect, and does not even touch the flags.
The AMD64 was designed for the LP64 model, as opposed to ILP64, i.e. in C, long and pointers are 64 bit, but integers are still 32 bit. So the AMD64 is optimized for 32 bit arithmetic: When working with 32 bit values in the now 64 bit wide registers, the upper 32 bits of the result are always cleared. So an “add eax, ebx” would add the lower 32 bits of (the 64 bit registers) RAX and RBX, and clear the upper 32 bits of the result.
So what does “xchg eax, eax” do? It does nothing… and then clears the upper 32 bits of EAX.
AMD decided that the opcode 0×90 should remain a NOP even in 64 bit mode of the AMD64 instruction set, so 0×90 is now an explicit NOP. If you really need to do “xchg eax, eax” and thus clear the upper 32 bits, you can use the two-byte encoding 0x87h, 0xC0 and this is what an assembler will generate.
Actually 0×90 has been a NOP since the i486. While “xchg reg, reg” usually took 3 cycles, “xchg eax, eax”, i.e. “nop” only took one. All later CPUs also recognized 0×90 as a NOP, because otherwise there would have been a nasty (read and write) dependency on EAX, which could stall the pipeline significantly a NOP shouldnt really wait for EAX being written by previous instructions, and following instructions reading EAX shouldnt have to wait for a NOP…
Michael
This entry was posted in [trivia][6] on [March 18, 2006][7] by [Michael Steil][8].
### Post navigation
←[ First assembly puzzle!][9] [Puzzle: PowerPC Flag Simulation on x86 →][10]
## 4 thoughts on “How NOP nearly became a non-NOP on AMD64”
1. **Norman Diamond** [November 27, 2007 at 00:10][11]
“The NOP instruction would now clear the upper 32 bits of EAX in 64 bit mode.”
s/EAX/RAX/
“So what does “xchg eax, eax” do? It does nothing… and then clears the upper 32 bits of EAX.”
set noic
s/EAX/RAX/
[Reply][12] ↓
2. **idknow** [February 23, 2011 at 16:38][13]
there are ONLY 32bits in the eax register, in a 32bit x86 cpu!
[Reply][14] ↓
3. **Akaina** [July 17, 2014 at 05:32][15]
AMD64 was an absolutely horrible and overhyped transition compared to the 16-to-32 of the 386. The monkeys at AMD decided to wipe out a whole row of single-byte increment and decrement instructions, nearly broke the NOP due to the decision to always clear the upper bits (the only thing more stupid would be to always sign-extend) leave those bits alone and wed get effectively double the number of 32-bit registers like you could do with ExX and xX and a rotate by 16, and in their misguided efforts removed things like SAHF/LAHF and segmentation, only to realise that these features are very useful for things like virtualisation and reluctantly put them back later on.
What they shouldve done is similar to how Intel handled the 16-to-32 transition: a prefix or two to access the upper register bank and make operands 64-bit, maybe even from real mode (like it was possible to use the 32-bit registers) if they were smart they couldve used the existing addr/oper override prefices), leave existing instructions and register contents unchanged with the exception of widening, and 64-bit address/data sizes being set in segment descriptors the same way 16/32-bit ones do theres enough previously reserved bits to do this (enough to make one wonder whether Intel reserved them with this purpose in mind!)
[Reply][16] ↓
4. Pingback: [TECNOLOGÍA » Odd ways to zeroing some x86][17]
### Leave a Reply [Cancel reply][18]
Your email address will not be published. Required fields are marked *
Name *
Email *
Website
Comment
You may use these HTML tags and attributes: `<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> `
Notify me of follow-up comments by email.
Notify me of new posts by email.
Search for:
### Recent Posts
* [Murdlok: A new old adventure game for the C64][19]
* [Commodore KERNAL History][20]
* [The Ultimate Apollo Guidance Computer Talk [video]][21]
* [The Ultimate Apollo Guidance Computer Talk @ 34C3][22]
* [62 Reverse-Engineered C64 Assembly Listings][23]
### github
### Blogroll
* [debugmode][24]
* [Alex Ionescu's Blog][25]
* [Fun with virtualization][26]
* [OS/2 Museum][27]
### Categories
* [6502][28]
* [archeology][29]
* [default][30]
* [digital video][31]
* [hacks][32]
* [literature][33]
* [puzzle][34]
* [SCUMM][35]
* [security][36]
* [tricks][37]
* [trivia][38]
* [Uncategorized][39]
* [whines][40]
### Meta
* [Log in][41]
* [Entries RSS][42]
* [Comments RSS][43]
* [WordPress.org][44]
[Proudly powered by WordPress][45]
[1]: http://www.pagetable.com/ "pagetable.com"
[2]: http://www.pagetable.com#content "Skip to content"
[3]: http://www.pagetable.com/
[4]: http://www.pagetable.com/?page_id=5
[5]: http://www.pagetable.com/?p=6#comments "Comment on How NOP nearly became a non-NOP on AMD64"
[6]: http://www.pagetable.com/?cat=12 "View all posts in trivia"
[7]: http://www.pagetable.com/?p=6 "01:10"
[8]: http://www.pagetable.com/?author=1 "View all posts by Michael Steil"
[9]: http://www.pagetable.com/?p=340
[10]: http://www.pagetable.com/?p=7
[11]: http://www.pagetable.com/?p=6#comment-94
[12]: /?p=6&replytocom=94#respond
[13]: http://www.pagetable.com/?p=6#comment-95
[14]: /?p=6&replytocom=95#respond
[15]: http://www.pagetable.com/?p=6#comment-97
[16]: /?p=6&replytocom=97#respond
[17]: http://tecnologia.revistacocktel.com/odd-ways-to-zeroing-some-x86/
[18]: /?p=6#respond
[19]: http://www.pagetable.com/?p=940
[20]: http://www.pagetable.com/?p=926
[21]: http://www.pagetable.com/?p=922
[22]: http://www.pagetable.com/?p=919
[23]: http://www.pagetable.com/?p=904
[24]: http://debugmo.de
[25]: http://www.alex-ionescu.com
[26]: http://virtuallyfun.superglobalmegacorp.com
[27]: http://www.os2museum.com/wp/
[28]: http://www.pagetable.com/?cat=2 "View all posts filed under 6502"
[29]: http://www.pagetable.com/?cat=3 "View all posts filed under archeology"
[30]: http://www.pagetable.com/?cat=4 "View all posts filed under default"
[31]: http://www.pagetable.com/?cat=5 "View all posts filed under digital video"
[32]: http://www.pagetable.com/?cat=6 "View all posts filed under hacks"
[33]: http://www.pagetable.com/?cat=7 "View all posts filed under literature"
[34]: http://www.pagetable.com/?cat=8 "View all posts filed under puzzle"
[35]: http://www.pagetable.com/?cat=9 "View all posts filed under SCUMM"
[36]: http://www.pagetable.com/?cat=10 "View all posts filed under security"
[37]: http://www.pagetable.com/?cat=11 "View all posts filed under tricks"
[38]: http://www.pagetable.com/?cat=12 "View all posts filed under trivia"
[39]: http://www.pagetable.com/?cat=1 "View all posts filed under Uncategorized"
[40]: http://www.pagetable.com/?cat=13 "View all posts filed under whines"
[41]: http://www.pagetable.com/wp-login.php
[42]: http://www.pagetable.com/?feed=rss2 "Syndicate this site using RSS 2.0"
[43]: http://www.pagetable.com/?feed=comments-rss2 "The latest comments to all posts in RSS"
[44]: http://wordpress.org/ "Powered by WordPress, state-of-the-art semantic personal publishing platform."
[45]: http://wordpress.org/ "Semantic Personal Publishing Platform"
[*HTML]: HyperText Markup Language
[*RSS]: Really Simple Syndication