0
\$\begingroup\$

I'm trying to debug a program running on an ATtiny84A, and I am having trouble understanding either how to use it, or interpreting its output, or both. For testing purposes, I created a simple program that I have uploaded to the microcontroller which simply sets the value of a test variable unsigned char test_var=0xAA, and outputs a LOW to PORTA6. I then put the microcontroller into debug mode, and started debugging with GDB.

First I wanted to print the value of the test variable, so I typed print test_var, which output $9 = 170 '\252'

I am guessing $9 refers to the output count, and I know that 170 = 0xAA, but I don't know what '\252' means.

Next I changed the variable to a char instead of an unsigned char and ran the same print test_var, I got an output of $10 = -86 '\252' which doesn't make a lot of sense to me. As far as I was aware, when you have a signed char (or just char) the MSB of the byte indicates the sign of the number, so if there is a 1 at BIT7 of the byte, it means that its a negative number, so when you look at 0xAA you have 0b10101010. At BIT7 you have a 1 so that means that the number is negative, which makes sense; however, the preceding 7 bits 0b0101010, when converted to decimal is equal to 42, so the number SHOULD be -42, but according to GDB it's -86. What Am I understanding wrong with that?

Next I wanted to read the direction register DDRA since I set DDA6 as an output for PORTA6 to be set LOW. From this I would know that the DDA6 bit of DDRA should be a 1 so I wanted to print the contents of DDRA to see that.

First I tried print DDRA which output = 0 '\000' which doesn't make sense because I know that there should be a bit set at DDA6, and yet this is saying that no bits are set in that register. On top of that, there is a '\000' which is different from the '\252' from before.

I thought that maybe It needed the physical memory address of the register so I looked at the end of the ATtiny84A datasheet in the Register Summary section and saw that DDRA had memory address 0x1A (0x3A) (I am not sure what the second value in brackets refers to so I tried both), so I then tried print 0x1A which output = 26 and print 0x3A which output = 58 neither of which make sense because I should see 64 indicating that BIT6 of DDRA has been set. And on top of this, these outputs are missing the '\000' and '\252' from before.

Where is my understanding going wrong? I'm getting such different values with everything and I have absolutely no idea why, and I would love some help.

Thank you!

EDIT: Here is the code as requested, and the GDB transaction for the DDRA register.

Program code:

#include <avr/io.h>

char test_var = 0xAA; // Also tested with `unsigned char`

void main(void)
{
    DDRA |= (1 << DDA6);
    PORTA &=~ (1 << PORTA6);
    while(1)
    {
    }
}

GDB Output:

(gdb) print DDRA
$15 = 0 '\000'
(gdb) print 0x1A
$16 = 26
(gdb) print 0x3A
$17 = 58

and from the datasheet:

enter image description here

\$\endgroup\$
10
  • \$\begingroup\$ You should include your program and where you set the break point. A full transcript of your interaction with gdb would also help. \$\endgroup\$
    – ErikR
    Jun 5 at 3:45
  • \$\begingroup\$ @ErikR I have added the code. What do you mean about a transcript of interaction with GDB? just a list of commands sent? \$\endgroup\$
    – Kalcifer
    Jun 5 at 3:47
  • \$\begingroup\$ It's a screenshot of your interaction with GDB. It shows all of the commands you entered as well as what GDB printed back out to you. \$\endgroup\$
    – ErikR
    Jun 5 at 3:48
  • 1
    \$\begingroup\$ If those are all of the commands you entered into GDB then you never starting executing your program. test_var is set to 0xAA because it is initialized that way by the C runtime, but you never executed even a single line of code in main. \$\endgroup\$
    – ErikR
    Jun 5 at 3:54
  • 2
    \$\begingroup\$ When you say print 0x1A you are telling GDB to print the number 0x1A which is 26. You have to dereference a pointer which is pointing to address 0x1A. Something like print *((unsigned char*)0x1A) \$\endgroup\$
    – ErikR
    Jun 5 at 4:02
2
\$\begingroup\$

but I don't know what '\252' means.

This is an octal value. It's equal to 2 + 5*8 + 2*8*8 = 170.

Another example: '\123' is octal for 3 + 2*8 + 1*8*8 = 83.

Next I changed the variable to a char instead of an unsigned char and ran the same print test_var, I got an output of $10 = -86 '\252'

170 is greater than 127, so when represented as a char (which is a signed value) you'll see -86.

As far as I was aware, when you have a signed char (or just char) the MSB of the byte indicates the sign of the number, so if there is a 1 at BIT7 of the byte, it means that its a negative number, so when you look at 0xAA you have 0b10101010. At BIT7 you have a 1 so that means that the number is negative, which makes sense; however, the preceding 7 bits 0b0101010, when converted to decimal is equal to 42, so the number SHOULD be -42,

To determine the value of 0xAA as a signed 8-bit number, figure out how much you have to add to get to 0x00 and that's the negative value.

For instance, 0xAA + 0x56 = 0x00, so 0xAA is negative 0x56, or negative 86.

Another example: 0xFF as a signed number is the same as -1 because 0xFF + 1 = 0.

Next I wanted to read the direction register DDRA since I set DDA6 as an output for PORTA6 to be set LOW. From this I would know that the DDA6 bit of DDRA should be a 1 so I wanted to print the contents of DDRA to see that.

Based on the discussion in the comments it appears you never started your program.

Here is a list of GDB commands to control execution. The main ones used are step and continue:

https://sourceware.org/gdb/onlinedocs/gdb/Continuing-and-Stepping.html

\$\endgroup\$
2
  • \$\begingroup\$ Ok - please include a listing of your program and a transcript of your GDB session. \$\endgroup\$
    – ErikR
    Jun 5 at 3:45
  • \$\begingroup\$ Please see the updated question. \$\endgroup\$
    – Kalcifer
    Jun 5 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.