News for hackers. Stuff that matters.
Re:Understanding the x86's Decimal Adjust after Addition (DA... by kens on Monday January 30, 2023 @11:19PM (#34591011)
Anyone have questions on the DAA instruction? This is a bit of a niche post, I have to admit.
Re:Understanding the x86's Decimal Adjust after Addition (DA... by jamesfinlayson on Monday January 30, 2023 @11:38PM (#34591197)
If BCD is the only thing it's applicable for I don't think it has much use - I can't imagine any modern compilers generating BCD instructions nor any hand-rolled assembly making use of it these days.

> Even Intel's x86 processors are moving away from the DAA instruction; it generates an invalid opcode exception in x86-64 mode.

That's the first I've heard of that so I'm guessing no one complained.

Re:Understanding the x86's Decimal Adjust after Addition (DA... by rvalles on Tuesday January 31, 2023 @12:57AM (#34591743)
I have used DAA recently, in my "load option ROM in RAM floppy tool"[0]. Specifically, I use it for printing numbers as hexadecimal[1], as unfortunately the ASCII standard is not arranged in a way as to facilitate this.

0. https://github.com/rvalles/optromloader/

1. https://github.com/rvalles/optromloader/blob/master/optromlo...

Re:Understanding the x86's Decimal Adjust after Addition (DA... by gfaure on Monday January 30, 2023 @11:39PM (#34591205)
I only remember the DAA instruction because it was the last instruction I implemented in my Game Boy emulator. Since it's mainly used for end-user display of game scores, for most games it doesn't lead to any behaviour problems apart from messed up numeric strings, even if not implemented.
Re:Understanding the x86's Decimal Adjust after Addition (DA... by MBCook on Monday January 30, 2023 @11:47PM (#34591278)
Ditto! I fished the CPU emulation on the emulator I’m writing around Christmas and I purposefully pushed DAA to the end of my instruction TODO list.

It’s not that bad conceptually but in practice making sure all the flags are set right by the other instructions so DAA would work is a bit of a pain.

Re:Understanding the x86's Decimal Adjust after Addition (DA... by dezgeg on Tuesday January 31, 2023 @05:56AM (#34593656)
Another GB emulator hobbyist here :) My dim recollection is that Pokemon uses BCD for in-game cash, so there you might be able to 'cheat' or either never buy anything if BCD stuff is left unimplemented :)
Re:Understanding the x86's Decimal Adjust after Addition (DA... by anticensor on Tuesday January 31, 2023 @01:09AM (#34591824)
Can you make a post on the ASCII adjust series too? I would have expected both in a single blog post, though.
Re:Understanding the x86's Decimal Adjust after Addition (DA... by kens on Tuesday January 31, 2023 @01:32AM (#34591961)
There's not a lot to say about the ASCII adjust instructions. I mention them in footnote 2. They are the same idea except they act on a single BCD digit. They aren't specifically for ASCII, but if you ignore the upper nybble, ASCII digit characters match the BCD values.
Re:Understanding the x86's Decimal Adjust after Addition (DA... by atq2119 on Tuesday January 31, 2023 @08:38AM (#34594846)
Since reverse engineering the 8086 is your thing, I was really hoping for this blog post to go into how the instruction is actually implemented. Is it all fixed function in the ALU? Is it micro-coded and if so, how?
Re:Understanding the x86's Decimal Adjust after Addition (DA... by userbinator on Tuesday January 31, 2023 @09:22AM (#34595313)
Given that it's a 4-cycle instruction (and a reg-reg add is 3), it seems rather quick to be microcoded.

(As an aside, Google gives absolutely horrible results if you search for "8086 DAA instruction timing". At least half the ones on the first page seem to be for the 8085, there's also this article itself and the discussion on HN, neither of which have the information.)

Re:Understanding the x86's Decimal Adjust after Addition (DA... by kens on Tuesday January 31, 2023 @11:36AM (#34597390)
This article is sort of a prelude to looking at the 8086's hardware. (I was looking at the Intel documentation and realized it was confusing and slightly wrong, so needed to figure out what is really happening.)

The 8086 implements the instruction in microcode, but like all the arithmetic operations, the interesting stuff happens in the ALU. In particular, the ALU hardware has a DAA instruction that figures out what correction value is needed and adjusts the flags appropriately. Another microcode instruction performs the addition. So the microcode is just four instructions long.