以子程序的形式改写assume cs:code data segment db 'welcome to masm!' db 2,36,113 data ends code segment start: mov ax,data mov ds,ax mov bx,0 mov di,16 mov ax,0b800h mov es,ax mov si,16012+352 mov cx,3 s: mov ah,ds:[di] push cx mov cx,16 s1: mov al,ds:[bx] mov es:[si],ax add si,2 inc bx loop s1 pop cx add si,128 inc di mov bx,0 loop s mov ax,4c00h int 21h code ends end start
时间: 2024-02-29 07:53:07 浏览: 31
Sure, here is the code rewritten as a subroutine:
```
data segment
db 'welcome to masm!'
db 2,36,113
data ends
code segment
start:
mov ax, data
mov ds, ax
call print_message ; call the print_message subroutine
mov ax, 4c00h
int 21h
print_message:
push ax
push bx
push cx
push di
push si
push es
mov bx, 0
mov di, 16
mov ax, 0b800h
mov es, ax
mov si, 16012+352
mov cx, 3
print_loop:
mov ah, ds:[di]
push cx
mov cx, 16
print_char_loop:
mov al, ds:[bx]
mov es:[si], ax
add si, 2
inc bx
loop print_char_loop
pop cx
add si, 128
inc di
mov bx, 0
loop print_loop
pop es
pop si
pop di
pop cx
pop bx
pop ax
ret
code ends
end start
```
The `print_message` subroutine takes care of printing the message to the screen, while the `start` routine sets up the data segment, calls the `print_message` subroutine, and then ends the program.
In addition, the `print_message` subroutine saves and restores the registers it uses to prevent them from being overwritten by other parts of the program.