Author Topic: A couple of quick questions regarding the flags register  (Read 14659 times)

0 Members and 1 Guest are viewing this topic.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
A couple of quick questions regarding the flags register
« on: September 21, 2010, 07:33:12 pm »
A couple of quick questions regarding the flags register:

  • Is H basically a carry flag for the right nibble of the accumulator? Meaning if I do
Code: (TI-ASM) [Select]
LD A,15
INC A
    it should be set, right?[/li]
  • Is V set when the 7th bit changes from one to zero? Meaning if I do
Code: (TI-ASM) [Select]
LD A, 128
DEC A
    should it be set?[/li]
« Last Edit: September 21, 2010, 10:36:17 pm by DJ Omnimaga »




Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: F
« Reply #1 on: September 21, 2010, 10:36:05 pm »
You should probably use better topic titles. I edited your topic name from "F" to "A couple of quick questions regarding the flags register"

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: F
« Reply #2 on: September 21, 2010, 10:38:32 pm »
A couple of quick questions regarding the flags register:

  • Is H basically a carry flag for the right nibble of the accumulator? Meaning if I do
Code: (TI-ASM) [Select]
LD A,15
INC A
    it should be set, right?[/li]
    [li]Is V set when the 7th bit changes from one to zero? Meaning if I do[/li]
Code: (TI-ASM) [Select]
LD A, 128
DEC A
    should it be set?[/li]
[/quote]
Mostly correct on both points. However, it should be clarified that the overflow is set if an addition/subtraction carries across the 127 to -128 mark (which it does in your example)

Edit:
Also, maybe this will make it clearer. If the result of the addition or subtraction (when you don't lock the result to 8 bits) is less than -128 or larger than 127, the overflow flag is set.
« Last Edit: September 21, 2010, 10:42:45 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: A couple of quick questions regarding the flags register
« Reply #3 on: September 22, 2010, 06:52:47 pm »
You should probably use better topic titles. I edited your topic name from "F" to "A couple of quick questions regarding the flags register"

Oh, okay. I was just bored :P

A couple of quick questions regarding the flags register:

  • Is H basically a carry flag for the right nibble of the accumulator? Meaning if I do
Code: (TI-ASM) [Select]
LD A,15
INC A
    it should be set, right?[/li]
    [li]Is V set when the 7th bit changes from one to zero? Meaning if I do[/li]
Code: (TI-ASM) [Select]
LD A, 128
DEC A
    should it be set?[/li]
[/quote]
Mostly correct on both points. However, it should be clarified that the overflow is set if an addition/subtraction carries across the 127 to -128 mark (which it does in your example)

Edit:
Also, maybe this will make it clearer. If the result of the addition or subtraction (when you don't lock the result to 8 bits) is less than -128 or larger than 127, the overflow flag is set.
[/quote]

Okay, thanks!
« Last Edit: September 22, 2010, 06:53:00 pm by Deep Thought »




SirCmpwn

  • Guest
Re: A couple of quick questions regarding the flags register
« Reply #4 on: September 22, 2010, 06:58:31 pm »
Oh, quick question:
The carry flag is set when a number overflows, right?  Like:
Code: [Select]
ld hl, 255
inc hl

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: A couple of quick questions regarding the flags register
« Reply #5 on: September 22, 2010, 07:00:16 pm »
The inc and dec instructions don't affect flags when used with 16-bit register pairs.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: A couple of quick questions regarding the flags register
« Reply #6 on: September 22, 2010, 07:02:06 pm »
Oh, I was wondering why those tutorials always skip over the flags for inc [16-bit reg]. Thanks!
« Last Edit: September 22, 2010, 07:02:18 pm by Deep Thought »




Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: A couple of quick questions regarding the flags register
« Reply #7 on: September 22, 2010, 07:05:25 pm »
For some reason, the carry flag is NOT set when a number overflows with inc/dec, not even an 8-bit one.

For example:

ld l, 255
inc l

does not affect the carry flag.
« Last Edit: September 22, 2010, 07:05:49 pm by Hot_Dog »

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
« Last Edit: September 22, 2010, 07:11:35 pm by Deep Thought »




Offline Iambian

  • Coder Of Tomorrow
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 739
  • Rating: +216/-3
  • Cherry Flavoured Nommer of Fishies
    • View Profile
Re: A couple of quick questions regarding the flags register
« Reply #9 on: September 22, 2010, 07:53:12 pm »
For some reason, the carry flag is NOT set when a number overflows with inc/dec, not even an 8-bit one.

For example:

ld l, 255
inc l

does not affect the carry flag.
That got me good when I first started learning Z80 ASM. For a quick reference, take a look at this thing that every Z80 ASM developer ought to have: http://www.ticalc.org/pub/text/z80/z80instr.txt

I frequently view it when I'm unsure what flags any given instruction will affect. A "." indicates that the flag is not affected and a "*" indicates that the flag will be affected by its definition, whatever that may be (like when you add two numbers together and the result is over 255, the carry flag will be set). It is rather convenient how they set it all up, too.

EDIT: More often than not, I'll view that file just to find out how many bytes that particular instruction will take up. Great for those labelless jumps like "jr $+N"
« Last Edit: September 22, 2010, 07:55:19 pm by Iambian »
A Cherry-Flavored Iambian draws near... what do you do? ...

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: A couple of quick questions regarding the flags register
« Reply #10 on: September 22, 2010, 08:02:59 pm »
Thanks! That'll be useful...

Oh, and I know Tim refers to T-states, but what's Lem?




Offline Iambian

  • Coder Of Tomorrow
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 739
  • Rating: +216/-3
  • Cherry Flavoured Nommer of Fishies
    • View Profile
Re: A couple of quick questions regarding the flags register
« Reply #11 on: September 22, 2010, 08:03:28 pm »
It's "Len" or "Length". That's the number of bytes that instruction takes.
EDIT: That's what I really use that file for :)
« Last Edit: September 22, 2010, 08:04:47 pm by Iambian »
A Cherry-Flavored Iambian draws near... what do you do? ...

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: A couple of quick questions regarding the flags register
« Reply #12 on: September 22, 2010, 08:06:37 pm »
Oh ... right.

And I've seen an M used for time somewhere. What's that? It seems to be 1/4 of T or something. Maybe it's just the length?




Offline Iambian

  • Coder Of Tomorrow
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 739
  • Rating: +216/-3
  • Cherry Flavoured Nommer of Fishies
    • View Profile
Re: A couple of quick questions regarding the flags register
« Reply #13 on: September 22, 2010, 08:14:08 pm »
Oh. Those are machine cycles. Since the shortest instruction is 4 T-cycles (I like to use the term "clock cycle" or CC for this), it might be easier to represent some instructions in terms of machine cycles. The only real benefit is dealing with smaller numbers, though I don't really like using them because of all those odd instructions that will not divide evenly with 4. Like your 2 byte ADD instructions (11CCs) and your two byte inc/dec instructions (6CCs).
A Cherry-Flavored Iambian draws near... what do you do? ...

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: A couple of quick questions regarding the flags register
« Reply #14 on: September 22, 2010, 08:19:36 pm »
Here's another reference I use all the time.  Just go to Appendices->Technical References and Datasheets ->Z80 Instruction Set.