Keil Logo

Memory Model

The most significant impact on code size and execution speed is the selected memory model. Compiling in the small model always generates the smallest, fastest code possible.

The SMALL directive instructs the compiler to use the small memory model. In this memory model, all variables are stored in the internal memory of the 8051 (unless they are explicitly located elsewhere).

Memory access to internal data memory is very fast (typically performed in 1 or 2 clock cycles), and the code generated is much smaller than that generated for the compact or large models. For example, the following loop:

for (i = 0; i < 100; i++)  {
  do_nothing ();
}

is compiled in small model and large model to demonstrate the differences in the generated code.

Code Generated
In Small Model
        ; FUNCTION func (BEGIN)
                        ; SOURCE LINE # 10
0000 E4                 CLR   A
0001 F500    R          MOV   i,A

0003            ?C0001:
0003 E500    R          MOV   A,i

0005 C3                 CLR   C
0006 9464               SUBB  A,#064H
0008 5007               JNC   ?C0004

                        ; SOURCE LINE # 12
000A 120000  E          LCALL do_nothing

                        ; SOURCE LINE # 13
000D 0500    R          INC   i



000F 80F2               SJMP  ?C0001

                        ; SOURCE LINE # 14
0011            ?C0004:
0011 22                 RET
        ; FUNCTION func (END)

CODE SIZE: 17 Bytes
Code Generated
In Large Model
        ; FUNCTION func (BEGIN)
                        ; SOURCE LINE # 10
0000 E4                 CLR   A
0001 900000  R          MOV   DPTR,#i
0004 F0                 MOVX  @DPTR,A
0005            ?C0001:
0005 900000  R          MOV   DPTR,#i
0008 E0                 MOVX  A,@DPTR
0009 C3                 CLR   C
000A 9464               SUBB  A,#064H
000C 500B               JNC   ?C0004

                        ; SOURCE LINE # 12
000E 120000  E          LCALL do_nothing

                        ; SOURCE LINE # 13
0011 900000  R          MOV   DPTR,#i
0014 E0                 MOVX  A,@DPTR
0015 04                 INC   A
0016 F0                 MOVX  @DPTR,A
0017 80EC               SJMP  ?C0001

                        ; SOURCE LINE # 14
0019            ?C0004:
0019 22                 RET
        ; FUNCTION func (END)

CODE SIZE: 26 Bytes

In small model, the variable i is maintained in internal data memory. The instructions to access i, MOV A,i and INC i, require only two bytes each of code space. In addition, each of these instructions executes in only one clock cycle.

In large model, the variable i is maintained in external data memory. To access i, the compiler must first load the data pointer and then perform an external memory access (see offset 0001h through 0004h in the above listing). These two instructions alone take 4 clock cycles. The code to increment i is found from offset 0011h to offset 0016h. This operation consumes 6 bytes of code space and takes 7 clock cycles to execute.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.