尝试使用以下汇编代码打印最大数字(a 和 b)。但它只打印“第一条”(欢迎)消息。我只想打印“第二条”(即最大的)消息。
[IsGreater.asm]
; Compares the two operand (A & B)
; Prints "YES" if "A > B"
; Prints "NO" if "A < B"
; Prints "EQUAL" if "A == B"
section .data;
Message: db "Welcome to comparision program.", 10;
MessageLength: equ $-Message;
AMessage: db "Output: A is greater than B.", 10;
AMessageLength: equ $-AMessage;
BMessage: db "Output: B is greater than A.", 10;
BMessageLength: equ $-BMessage;
section .bss;
; Dynamic variables
a: resb 4;
b: resb 5;
section .text;
global _start;
; -----------------
write:
mov eax, 4; sys_write
mov ebx, 1; file descriptor (output)
mov ecx, Message;
mov edx, MessageLength;
int 80h; sys_call (execute)
exit:
mov eax, 1; sys_exit
mov ebx, 0; set "no error"
int 80h; sys_call
printA:
mov eax, 4; sys_write
mov ebx, 1; file descriptor (output)
mov ecx, AMessage;
mov edx, AMessageLength;
int 80h; sys_call (execute)
printB:
mov eax, 4; sys_write
mov ebx, 1; file descriptor (output)
mov ecx, BMessage;
mov edx, BMessageLength;
int 80h; sys_call (execute)
_start:
jmp write;
; mov word [a], 4;
mov eax, [a];
; mov word [b], 5;
mov ebx, [b];
; Compare "a > b"
cmp eax, ebx;
jg printA; Jump IF Greater (OR) when "Zero Flag" ZF is "0".
jmp printB; Else print B
jmp exit;
电流输出:
$ ./更大
欢迎来到比较计划。
预期输出:
$ ./更大
欢迎来到比较计划。
输出:B 大于 A。
编译命令:$ nasm IsGreater.asm -o IsGreater.o -f elf -F STABS -g
链接命令:$ ld IsGreater.o -o IsGreater -m elf_i386
运行命令:$ ./IsGreater
还请建议如何重用相同的“写入”代码块(即“消息”变量)来打印另一个字符串,而不是声明新变量。 (“A消息”和“B消息”)。
提前致谢。
您的程序首先跳转至
write
。write
的是 exit
。
该计划将会失败。
它不会返回到
start
,因为你做了 jmp
。相反,您应该使用
call
指令来调用 write
,并在 ret
中使用 write
返回调用者。