Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Initialising a stack for a new thread

Initialising a stack for a new thread

Scheduled Pinned Locked Moved Clever Code
data-structures
9 Posts 6 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Timothy Baldwin
    wrote on last edited by
    #1

    void thread();

    ...

    static uint8_t stack[40];
    *(uint16_t *)(stack + sizeof(stack) - 2) = uint16_t(&thread);
    stack_pointer = stack + sizeof(stack) - 21;

    When the new thread was switched to the program would restart. The address is on the right place on the stack. The stack is large enough. Weird answer later.

    J 1 Reply Last reply
    0
    • T Timothy Baldwin

      void thread();

      ...

      static uint8_t stack[40];
      *(uint16_t *)(stack + sizeof(stack) - 2) = uint16_t(&thread);
      stack_pointer = stack + sizeof(stack) - 21;

      When the new thread was switched to the program would restart. The address is on the right place on the stack. The stack is large enough. Weird answer later.

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      Shouldn't this be under coding horrors?

      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      T P 2 Replies Last reply
      0
      • J Joe Woodbury

        Shouldn't this be under coding horrors?

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        T Offline
        T Offline
        Timothy Baldwin
        wrote on last edited by
        #3

        It would better fit under CPU design horrors. It turned out that I was writing the address in the wrong byte order. This otherwise little-endian system has a big-endian stack!

        G S 2 Replies Last reply
        0
        • T Timothy Baldwin

          It would better fit under CPU design horrors. It turned out that I was writing the address in the wrong byte order. This otherwise little-endian system has a big-endian stack!

          G Offline
          G Offline
          geoffs
          wrote on last edited by
          #4

          What cpu is this that the code was written for?

          T 1 Reply Last reply
          0
          • G geoffs

            What cpu is this that the code was written for?

            T Offline
            T Offline
            Timothy Baldwin
            wrote on last edited by
            #5

            Atmel AVR, specifically a ATMega168 micro-controler.

            G 1 Reply Last reply
            0
            • T Timothy Baldwin

              Atmel AVR, specifically a ATMega168 micro-controler.

              G Offline
              G Offline
              geoffs
              wrote on last edited by
              #6

              Thanks, Tim. I like to keep my brain aware of these little oddball processors. I mostly work with embedded stuff on TI DSPs or Microblaze soft-processors in FPGAs.

              1 Reply Last reply
              0
              • T Timothy Baldwin

                It would better fit under CPU design horrors. It turned out that I was writing the address in the wrong byte order. This otherwise little-endian system has a big-endian stack!

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                Timothy Baldwin wrote:

                This otherwise little-endian system has a big-endian stack!

                That seems more than a little bizarre!

                Steve

                1 Reply Last reply
                0
                • J Joe Woodbury

                  Shouldn't this be under coding horrors?

                  Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                  P Offline
                  P Offline
                  peterchen
                  wrote on last edited by
                  #8

                  Depends on who wrote it. "Subtle bugs" = mine "Coding Horrors" = everyone elses But anyway, with the low traffic it would be ok to merge the two boards. "Subtle Horrors", maybe?

                  We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                  blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist

                  G 1 Reply Last reply
                  0
                  • P peterchen

                    Depends on who wrote it. "Subtle bugs" = mine "Coding Horrors" = everyone elses But anyway, with the low traffic it would be ok to merge the two boards. "Subtle Horrors", maybe?

                    We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                    blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist

                    G Offline
                    G Offline
                    Gary R Wheeler
                    wrote on last edited by
                    #9

                    Coding Bugs: It's What's For Dinner!™

                    Software Zen: delete this;
                    Fold With Us![^]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups