我编写了一个简单的汇编程序,它从键盘读取两个字节。程序比较字节并打印输出,如果它们相等则更大或相等。
我使用的是64位机器,带有8字节寄存器,我相信内存也是8字节宽。
我的问题是,如何在寄存器中使用8位而不是全部64位?目前为了执行我想要的操作,我必须使用xor rax, rax
指令或mov rax, 0
指令清除寄存器中的所有64位。如果我只需加载我想要比较的8位并对这8位执行sub
操作就会更容易。
global _start
section .text
_start:
push 0
mov rbp,rsp
sub rsp,0x10
;Clear out contents
xor rax,rax
mov [rbp-0x8],rax
mov [rbp-0x10],rax
push intro
push 0x42
call print
mov rax, 0
mov rdi, 0
lea rsi, [rbp-0x8]
mov rdx, 2
syscall
mov rax, 0
mov rdi, 0
lea rsi, [rbp-0x10]
mov rdx, 2
syscall
mov rax, [rbp-0x8]
mov rbx, [rbp-0x10]
sub rax,rbx
je equal
jg firstlarger
jl secondlarger
equal:
push equalmessage
push 0xfg
call print
jmp exit
firstlarger:
push first
push 0x10
call print
jmp exit
secondlarger:
push second
push 0x11
call print
jmp exit
exit:
mov rax,60
xor rdi,rdi
syscall
print:
push rbp
mov rbp,rsp
sub rsp,0x0
mov rax,1
mov rdi,1
mov rsi, [rbp+0x18]
mov rdx, [rbp+0x10]
syscall
leave
ret 0x10
section .data
intro: db "Please enter 1 character, hit enter, then another, then hit enter", 10
equalmessage: db "They are equal",10
first: db "First is larger",10
second: db "Second is larger",10