What is meaning of .model small in 8086 programs?
Asked Answered
C

2

15

I am a beginner in 8086 assembly language. I can understand the logic used in the program and write small programs myself. But I just want to know what this does:

.model small
.stack 300h

What is explanation for .model small?

I am using masm.

Catholic answered 12/11, 2017 at 18:58 Comment(1)
c-jump.com/CIS77/ASM/Directives/D77_0030_models.htmTorsibility
C
16

With .model tiny you get a program where CS, DS, and SS are all pointing to the same 64KB of memory. The stack is placed in the highest region of this 64KB segment.

With .model small you get a program where CS points to a segment of its own, followed by the segment where DS and SS are pointing to. The stack is placed in the highest region of the SS segment.

The directive .stack 300h is telling MASM the size of the stack, so MASM can warn you when the rest of the program (data,bss,heap) would clash with the stack.

In both these models all access to data items is done using near pointers.

Chelate answered 12/11, 2017 at 20:11 Comment(2)
are SS and DS pointing to same location in .model small?Aesthetics
@AayushNeupane They do in the default case. The .model small directive comes with model options. One of those is that you can choose NEARSTACK or FARSTACK. The default choice of NEARSTACK groups the stack segment into a single physical segment (DGROUP) along with data. SS is assumed to equal DS. The other choice FARSTACK, that you would have to make explicitly, does not group the stack with DGROUP; thus SS does not equal DS.Chelate
W
5

Because of 8086 being a 16-bit architecture, it has difficulties in accessing more than 64 KB memory.

The most efficient way to use pointers is to use the dedicated 16-bit registers (like bx). However, when your program wants to access more than 64 KB, it has to use also segment registers (like es). To allow both ways of addressing, memory models were invented.

So, the directive .model small tells the assembler that you intend to use the small memory model - one code segment, one data segment and one stack segment - and the values of the segment registers are never changed.

It has the following effects:

  1. You are allowed to write the instruction retn (return from a near subroutine) as ret. Because the assembler knows that all your code is in the same segment, all your subroutines will be near (i.e. have a 16-bit address), and all ret instructions mean retn.

    Sounds silly and insignificant? Read on.

  2. If your code is scattered across several source files, you will have call instructions that call subroutines that the assembler doesn't know anything about. When you use a small memory model, it knows at least that each subroutine has a 16-bit address, and a near call opcode can be used.

    You could write code without declaring your memory model, but then you'd have to call near instead of just call.

  3. If all your source files declare .model small, the linker will take all the code segments and try to fit them all into 64 KB (the same for data segments). This may fail, if the stuff is too big.

Small assembly programs usually don't care about memory model - 64 KB code is more than enough to write complex programs, unless you're using a big external library. In such case, .model small can mean "I don't care about that memory model stuff, just use the default".

Williamson answered 12/11, 2017 at 20:12 Comment(1)
Not sure you are confusing small with tiny. Small can have a separate CS from DS. So it can address 64kb of code and 64kb of data (they can be the same segment, but not necessary). tiny limits everything to 64kb and by default CS=DS=ES=SS = PSP segment when control is transferred to the COM program.Chemoprophylaxis

© 2022 - 2024 — McMap. All rights reserved.