x86 assembly - multimodule programming (MASM/TASM)

2019-08-09 15:27发布

问题:

ok so here's the code:

assume cs:code, ds:data
data segment
  sname db 'Some Name','$'
  len equ $-sname
  ascii db 'a'-'A'

data ends
code segment
start:
  mov ax,data
  mov ds,ax

  cld
  lea si,sname

  do_this:
    lodsb

    cmp al,61h
    jae lowercase

    uppercase:
      cmp si,len
      je the_end
      jmp continue

    lowercase:
      mov bl,ascii
      sub ax,bx

      mov ds:[si-1],al
      cmp si,len
      je the_end

  continue:
  loop do_this

  the_end:
  mov ax,0000
  lea dx,sname
  mov ah,09h
  int 21h

  mov ax,4c00h
  int 21h

code ends
end start

Basically it just converts all lowercase letters of the string 'sname' into uppercase. My question is, how do i split this code into 2 modules, one of which will handle the string printing part. Specifically, i want an .asm module that handles the string conversion to uppercase, and one that handles :

  lea dx,sname
  mov ah,09h
  int 21h

I can't seem to find any good tutorials on this so if you could point me towards some, it would be much appreciated, and if not, a simple example of how to put 2 .asm modules together ( what directives are required, etc ) , would be great.

回答1:

Well, you simply move whatever code and data you want to move to a different assembly file and keep all the segment definitions. And then you add a few declarations with extrn and public:

file1.asm:

code segment

public convert
convert:
  ; code for the convert routine
  ret

code ends
end

file2.asm:

code segment

extrn convert:near
start:
  ; some code
  call convert
  ; some more code

code ends
end start

You might need to formally define subroutines:

convert proc
  ; code for the convert routine
  ret
convert endp


回答2:

At the top beneath .MODEL you would provide:

EXTRN     RANDOM:FAR

Then:

;
          .CODE
MAIN:
          .STARTUP
;
;     
          CALL      RANDOM 
;
;

In Random.asm:

;
          .CODE   
          ASSUME    DS:RAND_DATA
;
RANDOM    PROC      FAR PUBLIC USES CX DX DS,
                    LOWER:WORD, UPPER:WORD
                                        ;FUNCTION RANDOM(LOWER,UPPER)
                                        ;SAVE REGISTERS (USES LIST)
          PUSHF                         ;SAVE FLAGS
          MOV       AX,SEG RAND_DATA    ;SET DS-REGISTER TO POINT
          MOV       DS,AX               ;TO LOCAL DATA SEGMENT
;
;
          POPF                         ;RESTORE FLAGS

          RET                          ;RETURN (RANDOM)
RANDOM    ENDP                         ;END RANDOM

For one thing I'm no master of ASM. So give it a shot and let me know. This is from a working program but I had to do some cutting and pasting and there were multiple procedures so let me know how it works and I'll try my best to work it out.