3

I was reading about something in the assembler documentations of the AVR microcontroller and I usually come across a line used to describe how would an instruction affect specific flag in the status register.

let's take an example from ADD instruction page

H:
Rd3 • Rr3 + Rr3 • R3 ¯ + R3 ¯ • Rd3
set if there was a carry from bit 3; cleared otherwise

In this line
Rd3 • Rr3 + Rr3 • R3 ¯ + R3 ¯ • Rd3
How can I read this line, what do those symbols mean?

Muhammad Nour
  • 527
  • 6
  • 16

1 Answers1

4

To start with the formulas suffer from a typographical error on that HTML version of the documents (the pdf doesn't). The formula you quote should read:

$$\mathrm{Rd3}\cdot\mathrm{Rr3} + \mathrm{Rr3}\cdot\overline{\mathrm{R3}} + \overline{\mathrm{R3}} \cdot\mathrm{Rd3}$$

In that formula there are three values. \$\mathrm{Rd}\$ is the current value of the destination register, \$\mathrm{Rr}\$ is the current value of the source register, and \$\mathrm{R}\$ is the result of the instruction (e.g. for ADD it would be \$\mathrm{R}=\mathrm{Rd}+\mathrm{Rr}\$).

The number next the value represents which bit from the value that is used in the calculation. In this case it would be bit \$3\$ for each of the registers.

The operators are the standard Boolean operators for AND (\$\cdot\$) and OR (\$+\$).

If you add the inferred brackets (AND has precedence over OR in Boolean equations), you get:

$$(\mathrm{Rd3}\cdot\mathrm{Rr3}) + (\mathrm{Rr3}\cdot\overline{\mathrm{R3}}) + (\overline{\mathrm{R3}} \cdot\mathrm{Rd3})$$

Hopefully in that form it is quite easy to follow.

Tom Carpenter
  • 63,168
  • 3
  • 139
  • 196
  • AhhhA, and I was wondering why I couldn't read it! – Muhammad Nour Feb 14 '17 at 21:48
  • One more question is this is how the AVR's CPU decide when to set the flag, does it use a corresponding hardware to do this! in other words, where did this formula came from? – Muhammad Nour Feb 14 '17 at 21:52
  • @MuhammadNour it will be related to how the CPU calculates the flags internally. They may not use that exact combination of gates as there are many instructions that produce different flag results. You can actually derive the formula yourself if you sit down and work out what the flags represent. Certainly it should be possible to work backwards from the formula to see that it is correct. – Tom Carpenter Feb 14 '17 at 22:04