<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>endeavor &#187; Assembly Tutorials</title>
	<atom:link href="http://myw3b.net/blog/index.php/category/assembly-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://myw3b.net/blog</link>
	<description>pwning the rainbow since... 2010</description>
	<lastBuildDate>Mon, 23 Jan 2012 16:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Teaching Assembly with RAVM</title>
		<link>http://myw3b.net/blog/index.php/2011/06/teaching-assembly-with-ravm/</link>
		<comments>http://myw3b.net/blog/index.php/2011/06/teaching-assembly-with-ravm/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 00:53:04 +0000</pubDate>
		<dc:creator>endeavormac</dc:creator>
				<category><![CDATA[Assembly Tutorials]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://myw3b.net/blog/?p=511</guid>
		<description><![CDATA[This post is not a class on assembly. It is about a tool I use and hope others will find useful. An understanding of x86 assembly will help. What is the RAVM, and why create it? &#8230; <a class="more-link" href="http://myw3b.net/blog/index.php/2011/06/teaching-assembly-with-ravm/">More<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>This post is not a class on assembly. It is about a tool I use and hope others will find useful. An understanding of x86 assembly will help.</em></p>
<h3>What is the RAVM, and why create it?</h3>
<p>Learning how programs work at the assembly level is crucial towards gaining a holistic understanding of modern day computing. While studying Computer Science at the United States Military Academy, I was introduced to a fantastic piece of in-house developed software: the MARC and MARASM (<a href="http://www.eecs.usma.edu/research/">available publicly here</a>). The MARC is a virtual 16-bit CPU programmed in ADA. When paired with the MARASM, an assembler for the MARC, cadets can write, assemble, and run assembly programs with a simplistic toolchain. </p>
<p>The MARC is a perfect example of using simple applications geared towards education to teach concepts, not features. Students trying to learn new concepts need tools that just work. I wanted to borrow the concepts of the MARC and create a piece of software which could be used as a stepping stone towards x86 assembly. More specifically, I wanted:</p>
<ul>
<li>A more comprehensive, but not complicated, instruction set which more closely mimicked an x86 instruction set.</li>
<li>32-bit, little endian words.</li>
<li>A way to help students visualize what was happening in memory while their programs were running.</li>
<li>A code base programmed in C, making it more accessible for expansion and hacking by others.</li>
</ul>
<p>With these goals in mind, I created the RAVM, the Rainbowsandpwnies Assembler and Virtual Machine. The RAVM comes with three parts: assembler, disassembler, and virtual machine. Here&#8217;s how you can grab a copy of the RAVM in Ubuntu:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">sudo apt-get install git build-essential libncurses5-dev
git clone git://github.com/endeav0r/ravm.git
cd ravm
make</pre>
<p><span id="more-511"></span></p>
<h3>An example</h3>
<p>The RAVM comes with a few example assembly programs, but let&#8217;s start with our own. We will create a function that adds two numbers together and returns the result. We will then call our function to add together 5 and 7, and then stop.</p>
<pre class="brush: plain; title: ; notranslate">main :
    mov r0, 7
    push r0
    mov r0, 5
    push r0
    call sum     ; sum(5, 7)
    add rsp, 0x8 ; this is the cdecl call convention
    hlt

sum :
    push rbp
    mov rbp, rsp

    push r1      ; callee saves registers r1-r7

    mov r1, rbp  ; place second argument in r1
    add r1, 0xc
    mov r1, [r1]

    mov r0, rbp  ; place first argument in r0
    add r0, 0x8
    mov r0, [r0]

    add r0, r1   ; perform the addition

    pop r1       ; restore saved registers
    pop rbp

    ret ; return</pre>
<p><em>As of this writing, push and pop only accepts registers. The instruction set is still being expanded.</em></p>
<p>What we have here is a simple assembly program. Now let&#8217;s see where the RAVM really earns its money.</p>
<p>The vm that comes with RAVM features, &#8220;godmode.&#8221; Godmode is, in my opinion, the best way to visualize a program in memory. Let&#8217;s take a look.</p>
<p>We can assemble and run the above program by saving the contents in sum.asm and running the following commands</p>
<pre class="brush: plain; title: ; notranslate">./assembler sum.bin sum.asm
./vm -i sum.bin -g</pre>
<p>This will present us with the following screen:</p>
<p><img src="http://myw3b.net/blog/images/ravm-ss1.png" /></p>
<p>On the left side of the screen are the addresses for all available memory locations (the VM is currently running with 512 bytes of memory). Starting at address 0, highlighted in cyan is the image loaded from sum.bin. Highlighted in green is the current instruction pointed to by our instruction pointer. The last word in memory, highlighted in red, shows the memory location pointed to by rsp, our stack pointer. In yellow is the user cursor, movable by the arrow keys.</p>
<p>At the bottom of the screen, starting from the top-left of the bottom, we have the address of the cursor, the value of the instruction pointer, a disassembled description of the current instruction, and then the value of every general purpose register.</p>
<p>The user steps through the instruction by pressing (or holding) the &#8220;s&#8221; key. Let&#8217;s step forward in our program until we are sitting on the <strong>add r0, r1</strong> instruction.</p>
<p><img src="http://myw3b.net/blog/images/ravm-ss2.png" /></p>
<p>We are now introduced to two new colors, blue and purple. Blue shows us the space occupied by the stack. Purple shows us the memory pointed to by the base pointer.</p>
<p>As the user continues to step through the program, he/she is simultaneously presented with the entire program laid out and color-coded in memory, the next instruction to execute, and the value of all registers. I have found that after an explanation of how a computer works, a real-time visual learning aid answers many questions.</p>
<h3>Teaching security with the RAVM</h3>
<p>My favorite example program in the RAVM is that of a basic strlen buffer overflow, overwriting the return address to point back into the stack and execute attacker instructions. When the program executes as intended, it takes two strings, one as a password and one as simulated user input. A function is called, and the user input string is copied into a buffer with a strcpy. The two strings are then compared with strcmp, and if the two strings match a value in memory holding 0xdeadbeef is zeroed to 0&#215;00000000. If the two strings do not match, the memory location is not zeroed and the program terminates.</p>
<p>To assemble the buffer overflow example, run this command:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">./assembler buffer_overflow.bin buffer_overflow.asm string.asm</pre>
<p>Here&#8217;s a quick screenshot of RAVM godmode during the buffer overflow action, exploit in place, to get us started (vm memory size restored to 1024 bytes, the default):</p>
<p><img src="http://myw3b.net/blog/images/ravm-ss3.png" /></p>
<p>The jump that is about to execute on the stack will jump the user onto the instructions which execute after a successful strcmp. The creation of this simple, one instruction exploit requires the use of all three tools: assembler, disassembler and vm. Let&#8217;s take a look at the first several instruction of buffer_overflow.bin as they appear from ./disassembler:</p>
<pre class="brush: plain; title: ; notranslate">00000000      10000000020c  MOV  r0, 524 (0000020c)
00000006              3200  PUSH r0
00000008        3000000022  CALL 34 (0000002f)
0000000d      060800000004  ADD  rsp, 4 (00000011)
00000013      410000000001  CMP  r0, 1 (00000014)
00000019        2200000001  JE   1 (0000001f)
0000001e                80  HLT
0000001f      100000000234  MOV  r0, 564 (00000253)
00000025      100100000000  MOV  r1, 0 (00000025)
0000002b            130001  MOV  [r0], r1
0000002e                80  HLT
0000002f              3209  PUSH rbp
00000031            110908  MOV  rbp, rsp
00000034      0608ffffffec  ADD  rsp, -20 (00000020)
0000003a            110009  MOV  r0, rbp
0000003d      060000000008  ADD  r0, 8 (00000045)
00000043            120000  MOV  r0, [r0]
00000046              3200  PUSH r0
00000048            110009  MOV  r0, rbp
0000004b      0600ffffffec  ADD  r0, -20 (00000037)
00000051              3200  PUSH r0
00000053        30000000e9  CALL 233 (00000141)
00000058      060800000008  ADD  rsp, 8 (00000060)
0000005e            110009  MOV  r0, rbp
00000061      0600ffffffec  ADD  r0, -20 (0000004d)
00000067              3200  PUSH r0
00000069      100000000228  MOV  r0, 552 (00000291)</pre>
<p>Currently, constant values are followed by their offset in the instructions. <em>This isn&#8217;t required for all instructions. Work in progress.</em></p>
<p>We start by pushing the address of our user supplied string on the stack and making a function call to check it against the password. After some stack cleanup, we check the result for a 1, which indicates a successful match. On a successful match, we jump to the instructions at 0x0000001f to zero out memory. On an unsuccessful match, we simply halt the program.</p>
<p>The disassembler provides us with an easy way to see all of our instructions next to their assembled addresses (and the addresses they will hold in memory). The attacker can then run his program in memory and calculate the offset from where the stack will be located after a return address overflow to the instructions he needs executed. Finally, the attacker has to find some instruction he can write on the stack which will include no 0&#215;00 bytes. A carefully crafted attacker-supplied string which returns back into the stack and executes a JMP instruction does the trick.</p>
<p>It&#8217;s an interesting exercise in creative thinking to manipulate a system, the RAVM, in ways unintended.</p>
<h3>Conclusion</h3>
<p>That&#8217;s what I use to teach concepts in low-level programming and assembly. I&#8217;m interested in any suggestions, criticisms and feedback people have. If this is something you would like to use, everything is available under the GPL license. Please let me know how it goes!</p>
]]></content:encoded>
			<wfw:commentRss>http://myw3b.net/blog/index.php/2011/06/teaching-assembly-with-ravm/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>x86 Assembly for C Programmers 1.1, Reddit Follow-up</title>
		<link>http://myw3b.net/blog/index.php/2009/10/x86-fcp-1-1-reddit-follow-up/</link>
		<comments>http://myw3b.net/blog/index.php/2009/10/x86-fcp-1-1-reddit-follow-up/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 15:21:04 +0000</pubDate>
		<dc:creator>endeavormac</dc:creator>
				<category><![CDATA[Assembly Tutorials]]></category>

		<guid isPermaLink="false">http://myw3b.net/blog/?p=82</guid>
		<description><![CDATA[I posted x86 Assembly for C Programmers 1 to reddit and got some great feedback. There were a few things that were brought up, and I&#8217;m taking a minute to address some of them (I&#8217;m not &#8230; <a class="more-link" href="http://myw3b.net/blog/index.php/2009/10/x86-fcp-1-1-reddit-follow-up/">More<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I posted <a href="http://myw3b.net/blog/?p=7">x86 Assembly for C Programmers 1</a> to <a href="http://www.reddit.com/r/programming/comments/9wzut/assembly_for_c_programmers_1/">reddit</a> and got some great feedback. There were a few things that were brought up, and I&#8217;m taking a minute to address some of them (I&#8217;m not addressing everything).  Thanks to everyone out there who took the time to point out mistakes and make suggestions.<span id="more-82"></span></p>
<h3>Intel Assembly Comprehensive Cheat Sheet</h3>
<p><a href="http://www.reddit.com/user/danukeru">danukeru</a> from reddit posted a link to <a href="http://www.jegerlehner.ch/intel/opcode.html">this cheat sheet</a>, which lists x86 opcodes, what their abbreviations mean, and their Intel syntax. It is awesome, and I wish I had it earlier.</p>
<h3>A little more into lea</h3>
<p>This comes from <a href="http://www.reddit.com/user/jputnam">jputnam</a> at reddit as a suggestion.</p>
<p>A quick google search turns up <a href="http://www-scm.tees.ac.uk/users/u0000408/Instruct/_LEA.htm">a</a> <a href="http://www.intel.com/software/products/documentation/vlin/mergedprojects/analyzer_ec/mergedprojects/reference_olh/mergedProjects/instructions/instruct32_hh/vc150.htm">few</a> <a href="http://wiki.answers.com/Q/What_is_load_effective_address">articles</a> which are helpful. I especially like <a href="http://courses.ece.illinois.edu/ece390/archive/fall2001/books/labmanual/inst-ref-lea.html">this one</a> from the University of Illinois at Urbana-Champaign.</p>
<p>Basically, <strong>lea</strong> sets a <em><span style="text-decoration: underline;">register</span></em> equal to a value, after computations on that value are completed. Usually, this is an offset from another register. This is different from <strong>mov</strong>, which is used to set a <em><span style="text-decoration: underline;">memory location pointed to by a register</span></em> equal to a value.</p>
<p>Let&#8217;s say we want to set our register <strong>ecx</strong> equal to <strong>ebp+0&#215;12</strong>, because that&#8217;s where one of our stack variables are, and instead of using <strong>[ebp+0x12]</strong> everytime we want to refer to that stack variable, we&#8217;re going to use <strong>ecx</strong> (I have no idea why we would ever want to do this, but we&#8217;ll pretend). We would use the <strong>lea</strong> instruction to do this all very neatly in one line.</p>
<pre lang="asm">lea ecx, [ebp+0x12]</pre>
<p>If you&#8217;re still not entirely cleared up on <strong>lea</strong>, visit some of the links above. They do an excellent job explaining this instruction.</p>
<h3>Explain why eax is set to 0 at the end of main</h3>
<p>Another one from <a href="http://www.reddit.com/user/jputnam">jputnam</a>.</p>
<p>It is a common convention to set <strong>eax</strong> to the return value. Main returns 0. Therefor, we set <strong>eax</strong> to 0.</p>
<p>If you call a function and want to check its returned value, check <strong>eax</strong>.</p>
<h3>Spend a little more time explaing ebp</h3>
<p>This one comes from <a href="http://www.reddit.com/user/stevep98">stevep98</a> at reddit.</p>
<p>Given this C function:</p>
<pre lang="asm">void function ebp_explanation (int argument)
{
	int i;
}</pre>
<p>This is a simplistic example of how this function may look on the stack (<a href="http://ditaa.sourceforge.net/">ditaa</a> is cool):</p>
<p><em>Image has been accidentally deleted</em></p>
<p>0&#215;74 is memory we reserve for int i. Normally, we reserve this space by setting <strong>esp</strong> somewhere beneath 0&#215;74, so when we push things on to the stack they do not overwrite the memory at 0&#215;74.</p>
<p>We then set <strong>ebp</strong> somewhere consistent on our stack, and we leave it there. Convention is to set <strong>ebp</strong> to where it is in the above illustration. From there, when we want to refer to certain pieces of memory (IE stack variables), we refer to them by an offset from <strong>ebp</strong>. If we want to add 1 to int i, it will look like this:</p>
<pre lang="asm">add DWORD PTR [ebp-0x4], 0x1</pre>
<p>Like storing the return value in eax, this is a convention, but not a rule. In fact, when we take our same program from the first tutorial, give gcc the -O3 flag, and look at the disassembled code, we will see gcc decides to refer to int i in regards to <strong>esp</strong> instead of in regards to <strong>ebp</strong>.</p>
<h3>Stack Alignment</h3>
<p><a href="http://pwnguin.net/">jldugger</a> commented on stack alignment.</p>
<p>If you want some more information stack alignment, which I admittedly didn&#8217;t do a terrific job of explaining, a simple google search returns these two pages: ( <a href="http://www.fftw.org/doc/Stack-alignment-on-x86.html">one</a> | <a href="http://stackoverflow.com/questions/672461/what-is-stack-alignment">two</a> ). They should help clear things up. I&#8217;m going to leave it at that for stack alignment.</p>
<h3>What&#8217;s to come</h3>
<p>I won&#8217;t know 100% what will come next until it&#8217;s written, but these are the next three topics I would like to hit (remember, I&#8217;m learning as I&#8217;m writing):</p>
<ol>
<li>Take one.c (the example program from x86 Assembly for C Programmers 1) compiled with -O2 and -O3, and analyze why the compiler does what it does with different levels/kinds of optimization.</li>
<li>Begin looking at some more complicated examples, more of the instruction set, and expand our knowledge of the x86 instruction set.</li>
<li>We will take a C program, break it down in to assembly, and begin optimizing it in assembly. This will be the first installment of writing/modifying the assembly code.</li>
</ol>
<p>Beyond that, we&#8217;ll find out when we get there.</p>
]]></content:encoded>
			<wfw:commentRss>http://myw3b.net/blog/index.php/2009/10/x86-fcp-1-1-reddit-follow-up/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>x86 Assembly for C Programmers 1</title>
		<link>http://myw3b.net/blog/index.php/2009/10/assembly-for-c-programmers-1/</link>
		<comments>http://myw3b.net/blog/index.php/2009/10/assembly-for-c-programmers-1/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 12:00:30 +0000</pubDate>
		<dc:creator>endeavormac</dc:creator>
				<category><![CDATA[Assembly Tutorials]]></category>

		<guid isPermaLink="false">http://myw3b.net/blog/?p=7</guid>
		<description><![CDATA[Introduction I&#8217;m writing a series of tutorials on x86 assembly for C programmers who are already familiar with many of the basics of programming and computing. The assembly tutorials available online just aren&#8217;t doing it for &#8230; <a class="more-link" href="http://myw3b.net/blog/index.php/2009/10/assembly-for-c-programmers-1/">More<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>I&#8217;m writing a series of tutorials on x86 assembly for C programmers who are already familiar with many of the basics of programming and computing. The assembly tutorials available online just aren&#8217;t doing it for me, and I need something organized the way I think, on the topics I&#8217;m interested in, presented in a way which make comprehensive understanding easy. I&#8217;ll do the work, go find the answers, and then drop everything here for you to enjoy.</p>
<p>Please note I do not claim to be an expert on the assembly language.</p>
<p>My interest in assembly is for both optimizing C applications, and the purpose of developing exploits for vulnerabilities in common applications, <em>not</em> write applications in assembly from scratch. I&#8217;m not interested in, &#8220;Good,&#8221; examples of assembly, I&#8217;m interested in real examples. This will affect the assembly we look at. More specifically, I write the code in C, compile it with gcc, and what comes out is what we&#8217;ll be dissecting.</p>
<p>For the purposes of these tutorials, 32-bit x86 assembly. Everything compiled/built/disassembled on the latest stable distro of Ubuntu.<span id="more-7"></span></p>
<h3>References</h3>
<p><a href="http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html">The Art of Assembly</a> is an excellent reference, and if you need clarification of any of the topics discussed, I recommend checking it out. <a href="http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html#top">Chapter six</a> covers all of the instructions, how they work, and what specifically they do.</p>
<h3>Thanks To:</h3>
<p>Bushmills from irc.freenode.net##asm for taking the time to explain to a noob why the first 7 lines of assembly were what they were.</p>
<h3>The Code</h3>
<p>Let&#8217;s take a look at a simple C application, and it&#8217;s disassembled assembly code.<br />
gcc one.c -o one</p>
<pre lang="c">#include <stdio.h>

int main (int argc, char * argv [])
{

	int i;

	argc++;

	for (i = 0; i < 10; i++)
		printf("%d\n", i);

	return 0;

}</pre>
<p>Disassembled counterpart (for main):<br />
objdump -d one -M intel</p>
<pre lang="asm">080483c4 :
 80483c4:	8d 4c 24 04          	lea    ecx,[esp+0x4]
 80483c8:	83 e4 f0             	and    esp,0xfffffff0
 80483cb:	ff 71 fc             	push   DWORD PTR [ecx-0x4]
 80483ce:	55                   	push   ebp
 80483cf:	89 e5                	mov    ebp,esp
 80483d1:	51                   	push   ecx
 80483d2:	83 ec 24             	sub    esp,0x24
 80483d5:	83 01 01             	add    DWORD PTR [ecx],0x1
 80483d8:	c7 45 f8 00 00 00 00 	mov    DWORD PTR [ebp-0x8],0x0
 80483df:	eb 17                	jmp    80483f8
 80483e1:	8b 45 f8             	mov    eax,DWORD PTR [ebp-0x8]
 80483e4:	89 44 24 04          	mov    DWORD PTR [esp+0x4],eax
 80483e8:	c7 04 24 d0 84 04 08 	mov    DWORD PTR [esp],0x80484d0
 80483ef:	e8 04 ff ff ff       	call   80482f8

 80483f4:	83 45 f8 01          	add    DWORD PTR [ebp-0x8],0x1
 80483f8:	83 7d f8 09          	cmp    DWORD PTR [ebp-0x8],0x9
 80483fc:	7e e3                	jle    80483e1
 80483fe:	b8 00 00 00 00       	mov    eax,0x0
 8048403:	83 c4 24             	add    esp,0x24
 8048406:	59                   	pop    ecx
 8048407:	5d                   	pop    ebp
 8048408:	8d 61 fc             	lea    esp,[ecx-0x4]
 804840b:	c3                   	ret
 804840c:	90                   	nop
 804840d:	90                   	nop
 804840e:	90                   	nop
 804840f:	90                   	nop</pre>
<p>This is a list of the instructions that are used above. We'll explain which each of these instructions do as we come across them later:</p>
<ul>
<li><strong>lea</strong> - Load Effective Address</li>
<li><strong>and</strong> - logical AND</li>
<li><strong>push</strong> - PUSH data on to the stack</li>
<li><strong>mov</strong> - MOVe data from one register to another</li>
<li><strong>sub</strong> - SUBtract</li>
<li><strong>jmp</strong> - JuMP</li>
<li><strong>call</strong> - CALL another subfunction</li>
<li><strong>add</strong> - ADDition</li>
<li><strong>cmp</strong> - CoMPare</li>
<li><strong>pop</strong> - POP data off the stack</li>
<li><strong>ret</strong> - Return control to the parent function</li>
</ul>
<p>You'll notice we left off <strong>jle</strong>. <strong>jle</strong> means jump if less than or equal to, and is a variant of the <strong>jmp</strong> instruction. You can find all the variations with any assembly reference.</p>
<p>Now let's take a look at the registers used. (<a href="http://stackoverflow.com/questions/1395591/what-is-exactly-the-base-pointer-and-stack-pointer-to-what-do-they-point">ESP/EBP</a>)</p>
<ul>
<li><strong>esp</strong> - Stack Pointer (for the top of the stack).</li>
<li><strong>ecx</strong> - Counter (used for other purposes described later)</li>
<li><strong>ebp</strong> - Base Pointer</li>
<li><strong>eax</strong> - Accumulator Register (Arithmetic Operations)</li>
</ul>
<p>If you don't understand exactly what all these registers are, we'll describe them later, and you will see how they are used.</p>
<h3>Some Background:</h3>
<p>First, some vocabulary:</p>
<ul>
<li><strong>Stack</strong>: This is, surprise, an implementation of the data structure known as the <a href="http://en.wikipedia.org/wiki/Stack_(data_structure)">stack</a>. We use this stack to keep track of information about the program during the course of its running.</li>
<li><strong>Register</strong>: Think of <a href="http://en.wikipedia.org/wiki/Processor_register">registers</a> as our variables. Think of them as pointers, and we dereference them by putting them in [].</li>
<li><strong>Instruction</strong>: An i<a href="http://en.wikipedia.org/wiki/Instruction_(computer_science)">nstruction</a> is an operation we want to run on the processor.</li>
<li><strong>Operand</strong>: Quite simply, an <a href="http://en.wikipedia.org/wiki/Operand#Computer_science">operand</a> is an argument to an instruction.</li>
<li><strong>Word</strong>: Every 4 bytes is considered a word. <a href="http://en.wikipedia.org/wiki/Word_(computing)">Wikipedia defines word as the smallest unit of data used by a computer design</a>. We're using a 32 bit operating system, so 32 bit words, 4 byte words...</li>
</ul>
<p><span style="text-decoration: underline;"><strong>The x86 Stack and esp<br />
</strong></span></p>
<p>The x86 stack is a LIFO mechanism we use to store information, LIFO being Last In, First Out. <strong>push</strong> puts data on the stack, <strong>pop</strong> takes data off the stack. <strong>push</strong> and <strong>pop</strong> manipulate data relative to <strong>esp</strong>, which is the stack pointer.</p>
<p>The stack grows down, meaning we start at higher memory addresses, and as the stack grows, we end up with lower memory addresses. <strong>esp</strong> is often referred to as pointing to the top of the stack, but in diagrams, the top of the stack is depicted as at the bottom (because we have higher addresses at the top, and lower addresses at the bottom).</p>
<p><strong>esp</strong> decrements before adding a value to the stack, not after, so <strong>esp</strong> will always point to the last element added to the stack.</p>
<p>This may be a bit confusing now, but by the end of the first 7 instructions, you should have a good handle on it.</p>
<p>When we call a function, the stack typically looks like this:</p>
<pre>------------------
| argument 1     |
------------------
| argument 0     |
------------------
| return address | &lt;- esp is here
------------------</pre>
<p>This is how the function will inherit the stack. In most simplistic tutorials, a few more commands will be executed at the beginning of the function to give us a stack like this:</p>
<pre>------------------------
| argument 1           |
------------------------
| argument 0           |
------------------------
| return address       |
------------------------
| original ebp         | &lt;- ebp points here
------------------------
| stack data variables | &lt;- esp is here
------------------------</pre>
<p><span style="text-decoration: underline;"><strong>Aligning the Stack</strong></span></p>
<p>This stack, as you will see, is nothing more than a bunch of memory in relation to <strong>esp</strong>, and <strong>esp</strong> is the only way we can identify where we are in the stack. If we change esp, we change our location in the stack, without using push or pop.</p>
<p>We want the stack to be "aligned", meaning we want our stack variables to start on a word whose address ends in 0, or the memory is evenly divisible by 16, however is easiest for you to think of it. This apparently speeds up the computation of some operations, but more importantly, with the introduction of SSE instructions (which work on 128 bits at once), having your variables aligned improperly can lead to some spectacular failures.</p>
<p>It all has to do with memory segmentation <em>(Edit: Not really. See jldugger's comment. Processor design is important here. Visit the following link to learn more. I'm going to go ahead and mark this under not too terribly important to understand. Keep reading, you'll be fine, I promise.)</em> If you're really interested, <a href="http://en.wikipedia.org/wiki/X86_memory_segmentation">do some reading</a>. For now, just know we want our stack to be properly aligned, and that's what gcc is doing in the first seven instructions.</p>
<h3>The Assembly<span style="text-decoration: underline;"><strong><br />
</strong></span></h3>
<p>We're going to go instruction by instruction, explaining what's happening, and looking at the stack, along with where our registers are, each step along the way.</p>
<table border="0">
<tbody>
<tr>
<td valign="top">The state of the stack when we enter main() can be found to the right.</p>
<p>As we go through the first seven instructions, the instruction and a description will be found on the left, while the state of the stack will be found on the right.</td>
<td valign="top" nowrap="nowrap">
<pre>     ------------------
0x80 | char * argv[]  |
     ------------------
0x7c |   int argc     |
     ------------------
0x78 |   ret addr     | &lt;- esp points here
     ------------------
0x74 |                |
     ------------------
0x70 |                |
     ------------------
0x6c |                |
     ------------------
0x68 |                |
     ------------------
0x64 |                |
     ------------------
     ~     ~   ~      ~
     ------------------
0x40 |                |
     ------------------</pre>
</td>
</tr>
</tbody>
</table>
<table border="0">
<tbody>
<tr>
<td valign="top">
<pre lang="asm">lea ecx,[esp+0x4]</pre>
<p>This is the Load Effective Address instruction.</p>
<p>Syntax of lea:</p>
<p><strong>lea</strong> dest, source</p>
<p>It loads the destination register with the source register, after completing any necessary computations. For us, it loads the address of <strong>esp</strong> +0x4 into <strong>ecx</strong>, meaning <strong>ecx</strong> will point to the address beneath <strong>esp</strong> on the stack. Our stack now looks like this:</td>
<td valign="top" nowrap="nowrap">
<pre>     ------------------
0x80 | char * argv[]  |
     ------------------
0x7c |   int argc     | &lt;- ecx points here now
     ------------------
0x78 |   ret addr     | &lt;- esp points here
     ------------------
0x74 |                |
     ------------------
0x70 |                |
     ------------------
0x6c |                |
     ------------------
0x68 |                |
     ------------------
0x64 |                |
     ------------------
     ~     ~   ~      ~
     ------------------
0x40 |                |
     ------------------</pre>
</td>
</tr>
</tbody>
</table>
<table border="0">
<tbody>
<tr>
<td valign="top">
<pre lang="asm">and esp,0xfffffff0</pre>
<p>This is the logical and instruction.</p>
<p>Syntax of and:</p>
<p><strong>and</strong> dest, source</p>
<p>It performs a binary and between the destination and the source, and saves the result in the destination. If you're not familiary with binary operations, you should probably take some time to familiarize yourself with them immediately. Here's what wikipedia has to say on <a href="http://en.wikipedia.org/wiki/Binary_and">AND</a>.</p>
<p>This is where we align the stack.</p>
<p>Now our stack looks like this:</td>
<td valign="top" nowrap="nowrap">
<pre>     ------------------
0x80 | char * argv[]  |
     ------------------
0x7c |   int argc     | &lt;- ecx points here
     ------------------
0x78 |   ret addr     |
     ------------------
0x74 |                |
     ------------------
0x70 |                | &lt;- esp points here now
     ------------------
0x6c |                |
     ------------------
0x68 |                |
     ------------------
0x64 |                |
     ------------------
     ~     ~   ~      ~
     ------------------
0x40 |                |
     ------------------</pre>
</td>
</tr>
</tbody>
</table>
<table border="0">
<tbody>
<tr>
<td valign="top">
<pre lang="asm">push DWORD PTR [ecx-0x4]</pre>
<p>Push "pushes" an item on to the stack.</p>
<p>Syntax of push:</p>
<p><strong>push</strong> data</p>
<p>Let's break down what we are pushing on the stack.</p>
<p>The brackets mean we are referring to the contents of the memory pointed to by <strong>ecx</strong>-0x4. This is the return address. So <strong>ecx</strong>-0x4 is 0x7c, but [<strong>ecx</strong>-0x4] is the return address.</p>
<p>DWORD PTR means were are referring to a 32 bit value. WORD PTR is 16 bits, BYTE PTR is 8 bits. The processor knows ecx is a 32 bit value, but because we are pushing the value at ecx, the processor needs to know how many bits, starting at ecx, to push.</p>
<p>Once this is completed, the stack will look like this:</td>
<td valign="top" nowrap="nowrap">
<pre>     ------------------
0x80 | char * argv[]  |
     ------------------
0x7c |   int argc     | &lt;- ecx points here
     ------------------
0x78 |   ret addr     |
     ------------------
0x74 |                |
     ------------------
0x70 |                |
     ------------------
0x6c |   ret addr     | &lt;- esp points here now
     ------------------
0x68 |                |
     ------------------
0x64 |                |
     ------------------
     ~     ~   ~      ~
     ------------------
0x40 |                |
     ------------------</pre>
</td>
</tr>
</tbody>
</table>
<table border="0">
<tbody>
<tr>
<td valign="top">
<pre lang="asm">push ebp</pre>
<p>We're pushing <strong>ebp</strong> on to the stack. We do this so at the end of the function, we can restore ebp to its original state.</td>
<td valign="top" nowrap="nowrap">
<pre>     ------------------
0x80 | char * argv[]  |
     ------------------
0x7c |   int argc     | &lt;- ecx points here
     ------------------
0x78 |   ret addr     |
     ------------------
0x74 |                |
     ------------------
0x70 |                |
     ------------------
0x6c |   ret addr     |
     ------------------
0x68 | original ebp   | &lt;- esp points here now
     ------------------
0x64 |                |
     ------------------
     ~     ~   ~      ~
     ------------------
0x40 |                |
     ------------------</pre>
</td>
</tr>
</tbody>
</table>
<table border="0">
<tbody>
<tr>
<td valign="top">
<pre lang="asm">mov ebp,esp</pre>
<p>Mov moves the value of one register in to another.Think of mov as "dest := source"</p>
<p>Syntax of mov:</p>
<p><strong>mov</strong> dest, source</p>
<p>Here, we moving the value of the <strong>esp</strong> register in to the <strong>ebp</strong> register. If you understand the purpose of the <strong>ebp</strong> register, you know we use it to refer to variables on the stack. In our c application, int i; is a stack variable. Variables on the heap are generally variables for whom we dynamically allocate memory, but for now this isn't important. Know that on our stack we are going to have room for the integer i.</p>
<p>We need a way to refer to this place on the stack consistently. To do this, we use the <strong>ebp</strong> register. This register points to the base of our stack in this function. Now if we want to refer to integer i, we refer to an offset of the stack relative to <strong>ebp</strong>. As we continue to go through the instructions, you will see <strong>[ebp-0x8]</strong>, which actually refers to integer i on the stack.</td>
<td valign="top" nowrap="nowrap">
<pre>     ------------------
0x80 | char * argv[]  |
     ------------------
0x7c |   int argc     | &lt;- ecx points here
     ------------------
0x78 |   ret addr     |
     ------------------
0x74 |                |
     ------------------
0x70 |                |
     ------------------
0x6c |   ret addr     |
     ------------------
0x68 | original ebp   | &lt;- esp and ebp point here
     ------------------
0x64 |                |
     ------------------
     ~     ~   ~      ~
     ------------------
0x40 |                |
     ------------------</pre>
</td>
</tr>
</tbody>
</table>
<table border="0">
<tbody>
<tr>
<td valign="top">
<pre lang="asm">push ecx</pre>
<p>Now we're pushing <strong>ecx</strong> on to the stack. The reason we are doing this can be found in the instructions 0x8048406 and 0x8048408. We will use this <strong>ecx</strong> to return <strong>esp</strong> to its original state before executing the ret instruction at the end of this function..</td>
<td valign="top" nowrap="nowrap">
<pre>     ------------------
0x80 | char * argv[]  |
     ------------------
0x7c |   int argc     | &lt;- ecx points here
     ------------------
0x78 |   ret addr     |
     ------------------
0x74 |                |
     ------------------
0x70 |                |
     ------------------
0x6c |   ret addr     |
     ------------------
0x68 | original ebp   | &lt;- ebp points here
     ------------------
0x64 |      0x7c      | &lt;- esp points here now
     ------------------
     ~     ~   ~      ~
     ------------------
0x40 |                |
     ------------------</pre>
</td>
</tr>
</tbody>
</table>
<table border="0">
<tbody>
<tr>
<td valign="top">
<pre lang="asm">sub esp,0x24</pre>
<p>Sub is short for subtract, and it subtracts the value on the right from the value on the left.</p>
<p>Syntax of sub:</p>
<p><strong>sub</strong> dest, source</p>
<p>Think like this: "dest -= source"</p>
<p>Now we subtract 0x24 from <strong>esp</strong>. This gives us our room for our stack variables. We only have one stack variable, and definitely do not need 9 words of space on the stack to make room for an integer, which under normal circumstances should be just 4 bytes, or one word in size. However, because this code was not compiled with any optimization flags, this is how gcc pieced everything together.</td>
<td valign="top" nowrap="nowrap">
<pre>     ------------------
0x80 | char * argv[]  |
     ------------------
0x7c |   int argc     | &lt;- ecx points here
     ------------------
0x78 |   ret addr     |
     ------------------
0x74 |                |
     ------------------
0x70 |                |
     ------------------
0x6c |   ret addr     |
     ------------------
0x68 | original ebp   | &lt;- ebp points here
     ------------------
0x64 |      0x7c      |
     ------------------
     ~     ~   ~      ~
     ------------------
0x40 |                | &lt;- esp points here now
     ------------------</pre>
</td>
</tr>
</tbody>
</table>
<p>The first seven instructions are the most confusing, and things become much simpler from here. Hopefully you have become familiar with the working of the stack. I'm going to omit stack pictures from the remainder of this tutorial.</p>
<pre lang="asm">add DWORD PTR [ecx],0x1</pre>
<p>The add instruction works like the sub instruction, except instead of subtraction we are working with addition.</p>
<p>Because of the brackets, we are not adding 1 to <strong>ecx</strong>, but instead to the memory pointed to by <strong>ecx</strong>. If you remember from our stack, <strong>ecx</strong> points to the first argument we passed to main, or int argc. If you remember from our C code, after declaring int i, we incremented argc. Well, here's the assembly instruction for that line of code.</p>
<p>DWORD PTR because integers are 4 bytes (int argc).</p>
<pre lang="asm">mov DWORD PTR [ebp-0x8],0x0</pre>
<p>Now we are entering our for loop. The first thing our for loop does is set int i equal to 0. Well, we know int i is a stack variable. We also know the common convention is to refer to stack variables as an offset from <strong>ebp</strong>. So guess where int i is on the stack? That's right, it's at <strong>ebp</strong>-0x8. Here we are setting int i equal to 0, the first part of our for loop.</p>
<pre lang="asm">80483df:   eb 17   jmp   80483f8</pre>
<p>The jmp instruction is used to "JuMP" from one place in the code to another. I included the two bytes which form this instruction because I wanted to point something out. While we see "jmp 80483f8", which makes this instruction look absolute, it's actually relative. We are jumping 0x17 bytes ahead. 0xdf + 0x02 + 0x17 = 0xf8. Why add the 0x02? Because this jmp instruction is two bytes, and the jump starts after the jmp instruction.</p>
<p><strong><em><span style="text-decoration: underline;">We're going to do some skipping around now</span></em></strong>. Instead of following the assembly from first instruction to last, I'd instead like to go through the assembly in the order the instructions will be executed.</p>
<pre lang="asm">80483f8:   83 7d f8 09   cmp   DWORD PTR [ebp-0x8],0x9</pre>
<p>The CoMPare instruction compares two values, and sets the x86 flags register appropriately. Yes, there's an x86 flags register. No, we aren't that concerned with it right now. Just know that the cmp instruction sets flags which correspond to a comparison between its two operands.</p>
<pre lang="asm">80483fc:   7e e3   jle   80483e1</pre>
<p>The Jump if Less than or Equal to instruction will execute a jmp instruction if the x86 flags register has the appropriate flags set, indicating the previous cmp instruction compared one value that was less than or equal to a second value.</p>
<p>We're beginning to see exactly how our for loop executes on the processor. After setting the initial value, we jump immediately down to the comparison, or for our for () statement, "i &lt; 10". The comparison actually comes out to "i &lt;= 9". If this condition holds true, we perform another jump to where the beginning of our for loop code would be.</p>
<pre lang="asm">80483e1:   8b 45 f8   mov   eax,DWORD PTR [ebp-0x8]</pre>
<p><strong>eax</strong> is one of our general purpose registers we haven't mentioned yet. Here, we are setting it equal to <strong>[ebp-0x8]</strong>, or int i.</p>
<pre lang="asm">80483e4:   89 44 24 04   mov   DWORD PTR [esp+0x4],eax</pre>
<p>We are preparing to call the function printf. Printf takes two arguments. Remember, arguments are with the first argument closest to the top of the stack, and the last argument closest to the bottom. We are now positioning arguments on the stack. Int i represents our second argument in our printf() function call, and we are placing it closest to the bottom of the stack here.</p>
<pre lang="asm">80483e8:   c7 04 24 d0 84 04 08   mov   DWORD PTR [esp],0x80484d0</pre>
<p>Here we are moving the value 0x80484d0 in to the memory where esp is currently located. We're placing this value into the stack without altering esp. You're probably wondering what is at memory address 0x80484d0. It's these 4 bytes:</p>
<p>0x25 0x64 0x0a 0x00</p>
<p>The C String equivalent would be "%d\n". I hope it looks familiar, because it's the first argument to our printf call.</p>
<pre lang="asm">80483ef:   e8 04 ff ff ff   call   80482f8</pre>
<p>And here we go ahead and make the printf call. The call instruction will do a few things. For simplicity's sake, we will say it pushes the address of the next instruction on to the stack (the return address for the next function/procedure), and then begins executing the assembly instruction at the specified location. <a href="http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-5.html#HEADING5-98">If you absolutely must know...</a></p>
<p>We don't really need to worry too much about this now. Just know that 0x80483f4 just got pushed on to the stack, and the next instruction that will be executed is 0x80482f8. When the procedure we call returns, its ret instruction will pop the return address off the stack, meaning the stack should by just as we left it before the call instruction.</p>
<pre lang="asm">80483f4:   83 45 f8 01   add   DWORD PTR [ebp-0x8],0x1</pre>
<p>After the printf(), and before we do our next comparison, we need to increment int i. This is where that tiny piece of magic happens.</p>
<p>After this add instruction, we're back to our cmp instruction. We've already covered this, so let's skip ahead to the remaining six instructions, starting at the memory location 0x80483fe.</p>
<pre lang="asm"> 80483fe:	b8 00 00 00 00       	mov    eax,0x0
 8048403:	83 c4 24             	add    esp,0x24
 8048406:	59                   	pop    ecx
 8048407:	5d                   	pop    ebp
 8048408:	8d 61 fc             	lea    esp,[ecx-0x4]
 804840b:	c3                   	ret</pre>
<p>You should be able to understand what's going on now in these last six lines. If not, here's a quick synopsis to help you on your way:</p>
<ul>
<li><strong>80483fe:</strong> Zero out eax... eax := 0</li>
<li><strong>8048403:</strong> Return esp to its original position, before we made room for stack variables. Need more help? Look at memory location 0x80483d2.</li>
<li><strong>8048406:</strong> Get ecx back off the stack.</li>
<li><strong>8048407:</strong> Set ebp to its original value before we entered the procedure. We're returning to our parent function, and it probably wants to know where its stack variables are.</li>
<li><strong>8048408:</strong> Set esp back to its original value when we entered the main() procedure.</li>
<li><strong>804840b:</strong> Return, which will pop the return address off the stack, and the next instruction executed will now be at that return address.</li>
</ul>
<p>Take another look at the assembly instructions. You should now understand all the basics of what is happening at the processor.</p>
<p>In the next tutorial, we'll take a better look at what exactly is happening, with a little less abstraction and a little more detail.</p>
]]></content:encoded>
			<wfw:commentRss>http://myw3b.net/blog/index.php/2009/10/assembly-for-c-programmers-1/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: basic (Requested URI is rejected)

Served from: myw3b.net @ 2012-02-05 21:01:10 -->
