; Example assembly language program -- adds two numbers ; Author: R. Detmer ; Date: revised 7/97 .386 .MODEL FLAT ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD INCLUDE io.h ; header file for input/output cr EQU 0dh ; carriage return character Lf EQU 0ah ; line feed .STACK 4096 ; reserve 4096-byte stack .DATA ; reserve storage for data ;定义数据 .CODE ; start of main program code _start: ;编程 INVOKE ExitProcess, 0 ; exit with return code 0 PUBLIC _start ; make entry point public END ; end of source code
时间: 2024-02-10 16:05:43 浏览: 29
这个汇编程序是一个简单的示例,演示了如何在汇编语言中实现加法运算和退出程序。
```
; Example assembly language program -- adds two numbers
; Author: R. Detmer
; Date: revised 7/97
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096
.DATA
a DD 5 ; first number
b DD 7 ; second number
result DD ? ; result of addition
.CODE
_start:
mov eax, a ; move first number into EAX register
add eax, b ; add second number to EAX register
mov result, eax ; store result in memory
print_str "The sum of ", a, " and ", b, " is ", result, cr, Lf
INVOKE ExitProcess, 0 ; exit with return code 0
print_str PROC STDCALL
push ebp
mov ebp, esp
pushad
mov edx, 0
.print_loop:
mov eax, [ebp + 8 + edx * 4]
cmp eax, 0
je .print_end
push eax
call print_int
add esp, 4
inc edx
jmp .print_loop
.print_end:
mov eax, [ebp + 8 + edx * 4]
cmp eax, cr
je .print_return
cmp eax, Lf
je .print_return
push eax
call print_char
add esp, 4
jmp .print_end
.print_return:
popad
mov esp, ebp
pop ebp
ret
print_str ENDP
print_char PROC STDCALL
push ebp
mov ebp, esp
push ebx
mov bl, byte ptr [ebp + 8]
mov eax, 1
mov ecx, esp
mov [ecx], bl
mov ebx, 0
mov edx, 1
int 0x80
pop ebx
mov esp, ebp
pop ebp
ret
print_char ENDP
print_int PROC STDCALL
push ebp
mov ebp, esp
push ebx
push esi
push edi
mov eax, [ebp + 8]
mov ebx, 10
xor edx, edx
mov esi, esp
cmp eax, 0
jge .int_positive
neg eax
mov ah, '-'
mov byte ptr [esi], ah
inc esi
.int_positive:
cmp eax, 0
jne .int_loop
mov byte ptr [esi], '0'
inc esi
jmp .int_end
.int_loop:
xor edx, edx
div ebx
add dl, '0'
mov byte ptr [esi], dl
inc esi
cmp eax, 0
jne .int_loop
.int_end:
mov byte ptr [esi], 0
mov edi, esi
dec esi
mov ebx, esp
.int_reverse:
cmp esi, edi
jge .int_return
mov al, byte ptr [esi]
mov byte ptr [esi], byte ptr [edi]
mov byte ptr [edi], al
dec edi
inc esi
jmp .int_reverse
.int_return:
push ebx
call print_str
add esp, 4
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret
print_int ENDP
PUBLIC _start
END
```
这个程序使用了 IO.h 头文件中的 PRINT_STRING, PRINT_CHAR 和 PRINT_INT 函数来打印输出。程序将两个数字相加,并将结果存储在 memory 中,然后将结果打印到控制台。最后,程序调用 ExitProcess 函数退出。