Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASM 8086 : How is coding with multiple code segments different from coding with one code segment?

I am a high school student, and this year I started learning assembly.

I am currently making a Pacman clone as my final project.
The only issue that I'm having is that my code is big, and the *.exe file is almost at 64KB.

So my question is, if I move to model medium, what do I need to do to make my code work with the extra code segment?

I think that I need to use Icall instead of call, and update the procedures since now every time that I call them both the segment and ip get pushed, but is there anything else that I need to do?

like image 828
Jordan Belfort Avatar asked May 22 '26 22:05

Jordan Belfort


2 Answers

If you work in a model with more than one code segment, every code pointer becomes a 32 bit pointer. That means:

  • instead of call and ret, you need to use call far and ret far (this is something the assembler might do for you)
  • call far pushes a four byte return address on the stack, so you need to adjust the offsets for fetching arguments
  • function pointers need to be 32 bit as they also need to contain the code segment

For your purpose, it might be enough to just use the small model, that is, one segment for code and one for data and stack. This way, your executable can be up to 128 kB in size. The small model is very similar to the tiny model in that you can mostly ignore segmentation unless you need to read/write data from the text segment.

like image 64
fuz Avatar answered May 26 '26 11:05

fuz


For medium model (multiple code segments, one data segment), jumps within a code segment will be small (16 bit offset), jumps between code segments will be far (32 bit segment:offset). For functions that are only called within the same code segment, they can be declared as near and a near call (16 bit offset) will be used. Example code for Masm 6.x, the call and return for nearfun uses near call and return, the call and return for farfun uses far call and return.

        .286
        .model  medium
        .stack  1024

        .code   one                     ;code segment named "one"
main    proc    far
        call    nearfun                 ;call near function
        call    farfun                  ;call far  function
        mov     ax,04c00h               ;exit to dos
        int     21h
main    endp

nearfun proc    near
        mov     ax,1
        ret
nearfun endp

        .code   two                     ;code segment named "two"
farfun  proc    far
        mov     ax,2
        ret
farfun  endp

        end     main
like image 31
rcgldr Avatar answered May 26 '26 12:05

rcgldr