assembly - NASM:变量没有改变
问题描述
我需要将第一个变量的值更改为第二个,但我编写的代码不起作用。我试过这个:
mov DWORD [enety_26500], enety_26501
但程序仍然打印我“你好,世界!”
任何人都可以帮忙吗?
global _main
extern _ExitProcess@4
extern _printf
section .text
_main:
call main
pop eax
push 0
call _ExitProcess@4
ret
print:
push ebp
mov ebp, esp
mov eax, [ebp+8]
push eax
call _printf
pop ebp
push 0
ret
main:
push ebp
mov ebp, esp
push enety_26500
call print
pop eax
mov DWORD [enety_26500], enety_26501
push enety_26500
call print
pop eax
pop ebp
push 0
ret
section .data
enety_26500:
dw 'Hello, world!', 10, 0
enety_26501:
dw 'Hello,', 10, 0
解决方案
不清楚你想做什么。看起来您正在尝试这样做(在 C 中,使用更清晰的名称):
const char *stra = "hello world";
const char *strb = "hello";
...
printf(stra);
stra = strb;
printf(stra);
问题是,您不能让标签告诉您字符串的位置,然后尝试更改标签。这没有任何意义——标签本身不是容器或位置,它只是……一个标签。将标签存储在 eax 中并更改 eax 或将标签存储在另一个变量中:
push DWORD PTR sptr
call printf
mov eax, strb
mov DWORD PTR sptr, eax
push DWORD PTR sptr
call printf
....
sptr:
.long stra
stra:
.string "hello world"
strb:
.string "hello"
反正就是这样!
编辑:只是出于兴趣:如果您想更改 stra,并且 stra 存储在可写数据段中(即:.data),您可以这样做:
mov BYTE PTR stra+5, 0
这将在“hello”部分之后写一个 0 并在该点终止字符串。直接在 stra 上调用 printf 会打印“hello”。
推荐阅读
- python-3.x - 从列表中减去 64 或 36 后,需要将列表 sys.getsizeof() 除以 8 或 4(取决于机器)
- reactjs - 如何重写@devexpress/dx-react-grid-material-ui 的样式?
- java - 如何使用默认构造函数和设置器生成 pojo
- git - 无法使用服务主体从发布管道访问 Azure Devops Git 存储库
- html - 如何使用 flex 在 bootstrap 4.1 中获取可滚动的标签内容
- php - 声明变量时无法访问类中的受保护变量
- python - 当列标题中有空格时,无法从 CSV 文件导入列
- linux - 如何制作与进程分离然后重新附加到它的bash脚本?
- ctags - ctags 将地址标记为行号
- c# - decimal.ToString("C") 在 Linux 上生成 ¤ 货币符号