[英]Printing a number in the stack with NASM Assembly
Learning NASM Assembly for 32-bit Ubuntu. 学习32位Ubuntu的NASM程序集。
Doing a function that simply prints the number you pushed into the stack before calling it: 在调用之前,执行一个简单地打印您推入堆栈的数字的函数:
SECTION .text
global main
main:
; -----------------------------------------------------------
; Main
; -----------------------------------------------------------
push 5
call print_number
; -----------------------------------------------------------
; Exit
; -----------------------------------------------------------
mov EAX,1
int 0x80
; -----------------------------------------------------------
; Prints a number
; -----------------------------------------------------------
print_number:
push EBP
mov EBP,ESP
mov EAX,4
mov EBX,0
mov ECX,[EBP + 8]
add byte [ECX],'0'
mov EDX,1
int 0x80
pop EBP
ret
Keeps giving me a segmentation fault error. 继续给我一个分段错误错误。
Let's see... when I call the function, the stack should look like this: 让我们看看...当我调用函数时,堆栈应如下所示:
Then I push EBP
: 然后我推
EBP
:
I want EBP
to contain the address of the stack's base, so I do this: 我希望
EBP
包含堆栈的地址,所以我这样做:
mov EBP, ESP
So basically EBP
is now an address to the stack's base. 所以基本上
EBP
现在是堆栈基地的地址。 Since ECX
needs to be the address of whatever I'm going to print, ECX
should be EBP
(the address of the stack's base) moved by 8 bytes so that it points to the 5
I want to print: 由于
ECX
需要是我打算打印的地址, ECX
应该是EBP
(堆栈基地址)移动8个字节,以便指向我要打印的5
:
mov ECX,[EBP + 8]
And to print it I convert it to ASCII first: 要打印它我首先将其转换为ASCII:
add byte [ECX],'0'
I can't see the problem here. 我在这里看不到问题。 Why am I getting a segmentation fault?
为什么我会出现分段错误?
mov ECX,[EPB+8]
moves the value at location EPB+8
to ECX
, which is not a valid address. mov ECX,[EPB+8]
将位置EPB+8
处的值移动到ECX
,这不是有效地址。 It's just 5
. 这只是
5
。 You could just do: 你可以这样做:
mov ECX,EPB
add ECX,8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.