1

Assume the following code in C:

long int a = 262143;

which in binary will be 111111111111111111 (18 bits). If an atmega328p register can hold 8 bits, how is the above represented in the register?

Compiled Assembly:

    .file   "a.c"
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__SREG__ = 0x3f
__tmp_reg__ = 0
__zero_reg__ = 1
    .text
.global main
    .type   main, @function
main:
    push r28
    push r29
    rcall .
    rcall .
    in r28,__SP_L__
    in r29,__SP_H__
/* prologue: function */
/* frame size = 4 */
/* stack size = 6 */
.L__stack_usage = 6
    ldi r24,lo8(-1)
    ldi r25,lo8(-1)
    ldi r26,lo8(3)
    ldi r27,0
    std Y+1,r24
    std Y+2,r25
    std Y+3,r26
    std Y+4,r27
    ldi r24,0
    ldi r25,0
/* epilogue start */
    pop __tmp_reg__
    pop __tmp_reg__
    pop __tmp_reg__
    pop __tmp_reg__
    pop r29
    pop r28
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 8.2.0"
2

It isn't. It's split into four 8-bit values, each representing a quarter of the bits. Each value is stored in a separate register or memory location (depending on what is being done with it).

Software then combines them together to act as a single 32-bit value.

  • Then how would a single instruction look like? isn't an instruction supposed to be 8 bits in length in this case? – Josh l Sep 15 '18 at 17:58
  • 1
    There is no "single instruction". You should compile some code and look at the resultant assembly language - you'll find it's many instructions long. This is why 32-bit CPUs are more efficient than 8 bit. – Majenko Sep 15 '18 at 17:59
  • I added the assembly code above, so I guess ldi r24,lo8(-1) ldi r25,lo8(-1) are doing that – Josh l Sep 15 '18 at 18:01
  • and ldi r26,lo8(3) since you're working with 18 bits not 16. 0xFF is -1 in two's complement, so you have 0x03 ff ff which is 0b111111111111111111. – Majenko Sep 15 '18 at 18:02
  • 2
    @Joshl No - instructions are in flash, not RAM, and flash is 16 bits wide. Each instruction either fits in one 16 bit word or two 16 bit words depending on the instruction. The CPU knows how "long" each instruction is by the fact that the instruction itself will contain information about what needs to be fetched from where. – Majenko Sep 15 '18 at 18:18

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.