• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
SonicD007

Assembly newb with questions on my code

8 posts in this topic

Hey everyone,

So this is my first time writing assembly code and I'm trying to understand everything I'm doing so far but I need a bit of help. First off, here's what my code looks like:

[code]


BITS 32

;ssize_t write(int fd, const void *buf, size_t n);
xor eax, eax ; Make eax zero for our null terminator
xor esp, esp ; Clear the stack (Is this a bad idea?)
push eax ; Push the null terminator to the stack
push 0x7273752F ; /usr
push 0x6E69622F ; /bin
push 0x6465672F ; /ged
push 0x7469 ; it
mov ebx, 1 ; Use stdout
mov eax, 4 ; Move 4 into eax for write call
mov edx, 15 ; length of 15
mov ecx, esp ; push the string into ecx
int 0x80 ; Do the system call

; void _exit(int status);
mov eax, 1 ;Exist system call
mov ebx, 0 ;Status is clean
int 0x80 ;Do the system call
[/code]

The comments are what I think each line does. I'm basically trying to push "/usr/bin/gedit" to the stack that way I can then move esp into the buffer, ecx. Yes I realize I'm not actually executing gedit, this is just a test for me to see if I can print the path out. I'm having trouble doing this though because when I use hexdump -C on this, my string has an 'h' in between each push I have there. So it would look like this:
"h/usrh/binh/gedit"

Any idea why is this happening and do I have my logic correct for what each line is doing?

Thanks Edited by SonicD007
0

Share this post


Link to post
Share on other sites
h just stands for hex. [i]push 40[/i] and [i]push 40h[/i] would have different meaning.
I'm not sure why hexdump prints it like this. If you want to be sure try to print stack to file.
0

Share this post


Link to post
Share on other sites
`xor esp, esp' seems like a horrible idea. Just leave it alone.

I haven't programmed using Liinux system calls in assembly, but I doubt very much that the name of the file is expected to be in the stack. You probably need to put just a pointer there.

You can probably write a trivial C program and use gdb and figure out how write() is actually implemented.
0

Share this post


Link to post
Share on other sites
[quote name='Goran Milovanovic' timestamp='1346539476' post='4975573']
Why use xor to set a register to zero? I mean, why not simply mov eax 0?
[/quote]
Self-xor is historically (and still is, usually) faster than moving zero into a register, most compilers do it and it's more or less an idiom nowadays.

And yes, by setting ESP to zero without saving it, you just destroyed the stack pointer and your stack is gone. In assembly there are some registers you simply should not mess with unless you know what you are doing or really need the extra scratch space. If possible, make use of as few registers as possible (the "nice" ones like EAX, ECX, etc...) and use memory for the rest, and optimize later on. Edited by Bacterius
2

Share this post


Link to post
Share on other sites
Thanks for the replies!

Nypyren, am I pushing the string in the correct order right now or do I have it backwards? The push opcode generates the 'h' character; will this effect my string output or will it print out as: "/usr/bin/gedit"?

Lastly, I just want to make sure my comments are correct for why I'm doing these instructions.

You guys have been really helpful!
0

Share this post


Link to post
Share on other sites
Here's what will happen in memory during these commands:

[code]
Stack: ??...

push eax ; Push the null terminator to the stack

Stack: 00 00 00 00 ??...

push 0x7273752F ; /usr

Stack: 2F 75 73 72 00 00 00 00 ??...

push 0x6E69622F ; /bin

Stack: 2F 62 69 6E 2F 75 73 72 00 00 00 00 ??...

push 0x6465672F ; /ged

Stack: 2F 67 65 64 2F 62 69 6E 2F 75 73 72 00 00 00 00 ??...

push 0x7469 ; it

Stack: 69 74 00 00 2F 67 65 64 2F 62 69 6E 2F 75 73 72 00 00 00 00 ??...

What you actually have in memory on the stack in ASCII representation: "it(null)(null)/ged/bin/usr(null)(null)(null)(null)"
[/code]

Notice that each time a push occurs, the new data is added to the LEFT side. This is how the stack works on x86 processors.

What you want to do is reverse the order of your pushes, then you should get the string you want pushed properly. Also, you won't need to push eax anymore since the "it" portion of your push includes two free null terminators.

[quote name='SonicD007' timestamp='1346555465' post='4975621']
The push opcode generates the 'h' character; will this effect my string output or will it print out as: "/usr/bin/gedit"?
[/quote]

The opcodes won't affect your output string. The program itself is stored in a different area of memory than the stack. Your push instructions are essentially copying the values from the memory representing your code to the memory representing the stack, and adjusting the value of ESP.


[quote name='SonicD007' timestamp='1346555465' post='4975621']
Lastly, I just want to make sure my comments are correct for why I'm doing these instructions.
[/quote]

Your comments for each individual instruction are generally correct except for two:
"xor esp, esp" - this is NOT clearing the stack. This is the same as setting a pointer to NULL in C. Except this is the special 'stack pointer' register, and setting it to NULL will screw up ALL stack-related operations.

"mov ecx, esp" - it's not "pushing". It's just setting the value of ecx to be equal to esp. Assembly doesn't differentiate between numbers and pointer types, but due to the meaning of the code up to that point, ECX will represent a pointer to the start of your string. Edited by Nypyren
0

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1346553938' post='4975619']
Self-xor is historically (and still is, usually) faster than moving zero into a register, most compilers do it and it's more or less an idiom nowadays.
[/quote]

Ah, I see. Thanks.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0