问题描述
我想打印一个范围的ASCII字符与此汇编程序。
我想只用寄存器来做到这一点,但一直没多少运气。一切看起来没什么问题,但我在汇编编程新手,可能错过了一些东西明显。任何有识之士将AP preciated。谢谢:)
强调文本的
。文本
。全球_start
_start:
MOVL $ 1,EDX%MOVL $ 65%EBX
start_loop:
ADDL $ 1,EBX%
MOVL $ 0x04的EAX%
INT 0x80的$
CMPL $ 126%EBX
JLE start_loop
JMP start_loop 出口
MOVL $ 0%EBX
MOVL $ 1,%eax中
INT 0x80的$
您调用的SYS_WRITE系统调用。 SYS_WRITE()有三个参数,输出装置的文件描述符(它应该是1标准输出),要被打印您存储值的缓冲区的地址,并且要被打印的数据的大小。所以,你必须存储在%EBX文件描述符,并在ECX%和%edx中的数据的大小缓冲区存放地址。要存储您可以使用下面的指令的文件描述符。
MOVL $ 1%EBX //存储1(标准输出)的EBX)
要保存您可以使用数据的大小:
MOVL $ 1%EDX //大小为1字节
现在,您可以选择存储缓冲区的地址,你需要把你的数据在内存中一些地方,需要记忆的地址存放在ECX%。假设您希望将数据存储在堆栈中它的自我,那么你可以这样做:
subl $ 4%ESP //获取4个字节的内存堆栈
在内存MOVL $ 65(%ESP)//将数据存储在哪里ESP点
MOVL%ESP,ECX%//店在ECX中的数据的地址
现在您可以发出INT 0x80的。
MOVL $ 04,%EAX //存储系统调用在EAX号
诠释$ 0x80的//发出陷阱中断
作为一个整体,你可以编写如下code:
MOVL $ 1%EBX
subl $为0x4,ESP%
MOVL $ 64(%ESP)
start_loop:
MOVL(%ESP),EAX%
ADDL $ 1,%eax中
MOVL%EAX(%ESP)
MOVL%ESP,ECX%
MOVL $ 1,EDX%
MOVL $ 0x04的EAX%
INT 0x80的$
MOVL(%ESP),EAX%
CMPL $ 126%EAX
JLE start_loop
ADDL $为0x4,ESP%
请参阅Linux系统来在调用第二部分,以了解更多有关寄存器和系统调用的使用情况。
I'm trying to print a range of ascii characters with this assembly program.I'm trying to do it using only the registers, but haven't been having much luck. Everything looks fine to me, but I'm a novice at assembly programming and might have missed something obvious. Any insight will be appreciated. Thanks :)
emphasized text .text .global _start
_start:
movl $1, %edx
movl $65, %ebx
start_loop:
addl $1, %ebx
movl $0x04, %eax
int $0x80
cmpl $126, %ebx
jle start_loop
jmp start_loop
exit
movl $0, %ebx
movl $1, %eax
int $0x80
You are invoking the sys_write system call. sys_write() takes three arguments, file descriptor of the output device(it should be 1 for stdout),address of the buffer where you stored the value to be printed, and the size of the data to be printed. So you have to store file descriptor in %ebx, and store address of the buffer in %ecx and size of the data in %edx. To store the file descriptor you can use the following instruction.
movl $1, %ebx // store 1 (stdout) in ebx)
To store the size of the data you can use:
movl $1, %edx // size is 1 byte
Now, you have to store the address of the buffer, you need to put your data in the memory some where and need to store the address of the memory in %ecx. Assume that you want store the data in the stack it self, then you can do like this:
subl $4, %esp // get 4 bytes of memory in the stack
movl $65, (%esp) // store data in the memory where esp points to
movl %esp, %ecx // store address of the data in the ecx
Now you can issue the int 0x80.
movl $04, %eax // store syscall number in eax
int $0x80 // issue the trap interrupt
As a whole you can write the following code:
movl $1, %ebx
subl $0x4, %esp
movl $64, (%esp)
start_loop:
movl (%esp), %eax
addl $1, %eax
movl %eax, (%esp)
movl %esp, %ecx
movl $1, %edx
movl $0x04, %eax
int $0x80
movl (%esp), %eax
cmpl $126, %eax
jle start_loop
addl $0x4, %esp
See Linux System Calls Part2 at http://www.rulingminds.com/syscallspart2 to know more about registers and system calls usage.
这篇关于打印范围ASCII字符从寄存器在x86汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!