If an embedded system coded in C is 8 or 16-bit, how will it manipulate 32-bit data types like int?

倾然丶 夕夏残阳落幕 提交于 2019-12-20 05:41:12

问题


I think I'm thinking about this the wrong way, but I'm wondering how an embedded system with less than 32-bits can use 32-bit data values. I'm a beginner programmer so go easy on me :)


回答1:


base 10

 0100  <- carry in/out  
 5432
+1177
======
 6609

never brought up in class but we can now extend that to two operations

100 
 32
+77
======
 09

 01
 54
+11
======
 66

and come up with the 6609 result because we understand that it is column based and each column treated separately.

base 2

 1111
+0011
=====

11110 
 1111
+0011
=====
10010

110 
 11
+11
=====
 10

111 
 11
+00
=====
100

result 10010

you can break your operations up into however many bits you want 8, 16, 13, 97 whatever. it is column based (for addition) and it just works. division you should be able to figure out, multiplication is just shifting and adding and can turn that into multiple operations as well

n bits * n bits = 2*n bits so if you have an 8 bit * 8 bit = 16 bit multiply you can use that on an 8 bit system otherwise you have to limit to 4 bits * 4 bits = 8 bits and work with that (or if no multiply then just do the shift and add).

base 2

    abcd
*   1101
========
    abcd
   0000
  abcd
+abcd
=========

which you can break down into a shifting and adding problem, can do N bits with a 4 or 8 or M bit processor/registers/alu

Or look at it another way, grade school algebra

(a+b)*(c+d) = ac + bc + ad + bd

mnop * tuvw = ((mn*0x100)+(op)) * ((tu*0x100)+(vw)) = (a+b)*(c+d)

and you should find that you can combine the with 0x100 terms and without, do those separately from the without putting together parts of the answer using an 8 bit alu (or 4 bits of the 8 bit as needed).

shifting should be obvious just move the bits over to the next byte or (half)word or whatever.

and bitwise operations (xor, and, or) are bitwise so dont need anything special just keep the columns lined up.

EDIT

Or you could just try it

unsigned long fun1 ( unsigned long a, unsigned long b )
{
    return(a+b);
}

00000000 <_fun1>:
   0:   1166            mov r5, -(sp)
   2:   1185            mov sp, r5
   4:   1d40 0004       mov 4(r5), r0
   8:   1d41 0006       mov 6(r5), r1
   c:   6d40 0008       add 10(r5), r0
  10:   6d41 000a       add 12(r5), r1
  14:   0b40            adc r0
  16:   1585            mov (sp)+, r5
  18:   0087            rts pc

00000000 <fun1>:
   0:   0e 5c           add r12,    r14 
   2:   0f 6d           addc    r13,    r15 
   4:   30 41           ret 

00000000 <fun1>:
   0:   62 0f           add r22, r18
   2:   73 1f           adc r23, r19
   4:   84 1f           adc r24, r20
   6:   95 1f           adc r25, r21
   8:   08 95           ret

bonus points if you can figure out these instruction sets.

unsigned long fun2 ( unsigned long a, unsigned long b )
{
    return(a*b);
}

00000000 <_fun2>:
   0:   1166            mov r5, -(sp)
   2:   1185            mov sp, r5
   4:   10e6            mov r3, -(sp)
   6:   1d41 0006       mov 6(r5), r1
   a:   1d40 000a       mov 12(r5), r0
   e:   1043            mov r1, r3
  10:   00a1            clc
  12:   0c03            ror r3
  14:   74d7 fff2       ash $-16, r3
  18:   6d43 0004       add 4(r5), r3
  1c:   70c0            mul r0, r3
  1e:   00a1            clc
  20:   0c00            ror r0
  22:   7417 fff2       ash $-16, r0
  26:   6d40 0008       add 10(r5), r0
  2a:   7040            mul r0, r1
  2c:   10c0            mov r3, r0
  2e:   6040            add r1, r0
  30:   0a01            clr r1
  32:   1583            mov (sp)+, r3
  34:   1585            mov (sp)+, r5
  36:   0087            rts pc



回答2:


An 8 bit system can perform 8 bit operations in a single instruction and single memory access, on such an 8 bit system, 16 and 32 bit operations require additional data accesses and additional instructions.

For example, typical architectures place arithmetic results in register (often an accumulator but some architectures are more_orthogonal_ and can use any register for results), and arithmetic overflow results in a carry flag being set in a status register. In operations larger that the native architecture, the code can inspect the carry flag in order to take the appropriate action in subsequent instructions.

So say for an 8 bit system you add 1 to 255, the result in the 8 bit accumulator will be zero, with the carry flag set; the next instruction can then add one to the upper byte of a 16 bit value in response to the carry flag. This can be made to ripple through to any number of bytes or words, so that a system can be made to process operations of arbitrary bit length above that of the underlying architecture just not in a single instruction operation.



来源:https://stackoverflow.com/questions/46935775/if-an-embedded-system-coded-in-c-is-8-or-16-bit-how-will-it-manipulate-32-bit-d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!