为什么这个NASM code打印我的环境变量? [英] Why does this NASM code print my environment variables?

查看:200
本文介绍了为什么这个NASM code打印我的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了一个计算机体系结构当然,这学期,其中,除其他事项外,我们一直在MIPS汇编涉足并在MARS模拟器运行它。如今,出于好奇,我开始与NASM在我的Ubuntu箱乱搞,并已基本上只是从教程拼凑起来的东西,得到了NASM是如何从MIPS不同的感觉。这里是code段目前我正在看:

I'm just finishing up a computer architecture course this semester where, among other things, we've been dabbling in MIPS assembly and running it in the MARS simulator. Today, out of curiosity, I started messing around with NASM on my Ubuntu box, and have basically just been piecing things together from tutorials and getting a feel for how NASM is different from MIPS. Here is the code snippet I'm currently looking at:

global _start

_start:

    mov eax, 4
    mov ebx, 1
    pop ecx
    pop ecx
    pop ecx
    mov edx, 200
    int 0x80
    mov eax, 1
    mov ebx, 0
    int 0x80

这是保存为TEST.ASM,并与组装NASM -f精灵TEST.ASM LD -o测试test.o链接。当我与 ./测试anArgument 调用它,它打印anArgument',符合市场预期,随后然而,许多角色需要填充该字符串200个字符总数(因该 MOV EDX,200 语句)。有趣的事,不过,就是这些填充字符,我本来预计是乱码,其实都是来自我的环境变量的开始,由 ENV中显示命令。这是为什么打印出我的环境变量?

This is saved as test.asm, and assembled with nasm -f elf test.asm and linked with ld -o test test.o. When I invoke it with ./test anArgument, it prints 'anArgument', as expected, followed by however many characters it takes to pad that string to 200 characters total (because of that mov edx, 200 statement). The interesting thing, though, is that these padding characters, which I would have expected to be gibberish, are actually from the beginning of my environment variables, as displayed by the env command. Why is this printing out my environment variables?

推荐答案

不知道实际的答案或有时间去看看它,我猜的环境变量获得存储在内存中的命令行参数之后。您code简直是缓冲溢出到环境变量字符串进行打印了。

Without knowing the actual answer or having the time to look it up, I'm guessing that the environment variables get stored in memory after the command line arguments. Your code is simply buffer overflowing into the environment variable strings and printing them too.

这其实是有道理的,因为该命令行参数由系统/加载器处理,因为是环境变量,因此它是有道理的,它们的存储靠近对方。为了解决这个问题,你需要找到的命令行参数的长度,只能打印的字符。或者,因为我认为他们是空终止字符串,打印,直至达到一个零字节。

This actually makes sense, since the command line arguments are handled by the system/loader, as are the environment variables, so it makes sense that they are stored near each other. To fix this, you would need to find the length of the command line arguments and only print that many characters. Or, since I assume they are null terminated strings, print until you reach a zero byte.

编辑:
我假定这两个命令行参数和环境变量都存储在初始化数据段(在NASM。数据,我相信)

I assume that both command line arguments and environment variables are stored in the initialized data section (.data in NASM, I believe)

这篇关于为什么这个NASM code打印我的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆