    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Assembly Club</title>
        <link>https://members.accu.org/index.php/articles/2652</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 31, #2 - May 2019</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c398/">312</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-398/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+398/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Assembly Club</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 04 May 2019 23:29:37 +01:00 or Sat, 04 May 2019 23:29:37 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Ian Bruntlett compares dialects of assembly code.</p>
<p><strong>Body:</strong>&nbsp;<p>The first rule of Assembly Club is that no-one writes assembly. This article is not intended to teach anyone assembly language, but to help them on that journey. For these particular adventures, I used Ubuntu 18.04.2 LTS (64 bit, x86_64) on a refurbished ThinkPad.</p>

<p>Here are the packages I installed:</p>

<ul>
	<li>emacs â€“ the one true text editor</li>
	<li>make â€“ for building executables</li>
	<li>nasm â€“ a nice assembler</li>
	<li>yasm â€“ an even nicer assembler</li>
	<li>gas â€“ the assembler we have to put up with because it is available everywhere</li>
	<li>gdb â€“ the GNU debugger.</li>
</ul>

<p>When it comes to assembler, there are two major dialects â€“ Intel and AT&amp;T. Intel syntax is supported by the bulk of the tutorials and the GNU tools default to AT&amp;T syntax. However, they can be persuaded to accept Intel syntax (experience indicates it is a bit of a compromise).</p>

<p>The other reasons why programmers prefer Intel syntax over AT&amp;T syntax can be found from the manual:</p>

<ul>
	<li>AT&amp;T immediate operands are preceded by <code>$</code>; Intel immediate operands are undelimited (Intel <code>push 4</code> is AT&amp;T <code>pushl $4</code>). AT&amp;T register operands are preceded by <code>%</code>; Intel register operands are undelimited. AT&amp;T absolute (as opposed to PC relative) jump/call operands are prefixed by <code>*</code>; they are undelimited in Intel syntax.</li>
	
	<li>AT&amp;T and Intel syntax use the opposite order for source and destination operands. Intel <code>add eax, 4</code> is <code>addl $4, %eax</code>. The source, dest convention is maintained for compatibility with previous Unix assemblers. Note that instructions with more than one source operand, such as the enter instruction, do not have reversed order.</li>
	
	<li>In AT&amp;T syntax, the size of memory operands is determined from the last character of the instruction mnemonic. Mnemonic suffixes of <code>b</code>, <code>w</code>, <code>l</code> and <code>q</code> specify byte (8-bit), <code>word</code> (16-bit), <code>long</code> (32-bit) and <code>quadruple word</code> (64-bit) memory references. Intel syntax accomplishes this by prefixing memory operands (not the instruction mnemonics) with <code>byte ptr</code>, <code>word ptr</code>, <code>dword ptr</code> and <code>qword ptr</code>. Thus, Intel <code>mov al, byte ptr foo</code> is <code>movb foo, %al</code> in AT&amp;T syntax.</li>
	
	<li>Immediate form long jumps and calls are <code>lcall</code>/<code>ljmp $section, $offset</code> in AT&amp;T syntax; the Intel syntax is <code>call</code>/<code>jmp far section:offset</code>. Also, the <code>far</code> return instruction is <code>lret $stack-adjust</code> in AT&amp;T syntax; Intel syntax is <code>ret far stack-adjust</code>.</li>
	
	<li>The AT&amp;T assembler does not provide support for multiple section programs. Unix style systems expect all programs to be single sections.</li>
</ul>

<p>For me, I have a basic rule of thumb to handle the differences between Intel code and AT&amp;T code. For AT&amp;T syntax code, think of the comma between operands as â€˜toâ€™ and for Intel syntax code think of the comma as â€˜equalsâ€™.</p>

<h2>How did I get here?</h2>

<p>I learned assembly language on the Sinclair QL and Intel x86 processors. I am not an expert these days. I got to grips with Linux by reading <em>How Linux Works</em> <a href="#[1]">[1]</a> and <em>The Linux Command Line</em> <a href="#[2]">[2]</a>. I am currently reading two books and referring to online resources. The books are <em>Introduction to 64 bit Assembly Programming with Linux and OSX</em> <a href="#[3]">[3]</a> and <em>Low-Level Programming</em> <a href="#[4]">[4]</a>. As I progress, I am assembling a scrap book consisting of my findings.</p>

<h2>Versions of Hello World</h2>

<ul>
	<li>Listing 1 shows Hello World written for NASM/YASM
		<p>This program was built in stages. I used YASM to convert the assembler to an object file and the GNU linker (ld) to create an executable file. The parameters for YASM tell it to format its output as an ELF (Executable and Linking Format) file, with debug records in DWARF2 format (Debugging With Attributed Record Formats). The GNU linker takes the <span class="filename">hello.o</span> object file and creates an output executable called <code>program</code>.</p>
<pre class="programlisting">
  yasm -f elf64 -g dwarf2 hello.asm
  ld -o program hello.o</pre>
	</li>

	<li>Listing 2 shows Hello World written for the GNU assembler. It only runs on 64-bit Linux. I edited the original <a href="#[5]">[5]</a> to calculate string length when assembled.
		<p>I will explain how it gets built later on, using a makefile for GNU Make.</p>
	</li>
	
	<li>Listing 3 shows Hello World, written in part-Intel part-AT&amp;T syntax. This is so you can use the GNU assembler whilst almost writing your code in Intel format. A kind of poorly documented poor relation to the previous examples.
		<p>It is based on the â€˜hello worldâ€™ program from <em>Introduction to 64 bit Assembly Programming for Linux and OSX</em> <a href="#[3]">[3]</a>.</p>
	</li>

	<li> Listing 4 shows the Makefile for the previous examples. It is from the makefile for <a href="#[5]">[5]</a> and <a href="#[6]">[6]</a>.
		<p>It actually does a bit more than that. Typing <code>make hello-intel</code> makes the Intel/AT&amp;T variant, <code>make hello</code> makes the AT&amp;T variant.</p>
	</li>
</ul>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
%include &quot;syscalls.inc&quot;
global _start

section .data
message: db 'hello, world!', 10

section .text
_start:
  ; 1 system call number should be stored in rax
    mov     rax, __NR_write
  ; argument #1 in rdi: where to write (descriptor)?
    mov     rdi, 1
  ; argument #2 in rsi: where does the string start?
    mov     rsi, message
  ; argument #3 in rdx: how many bytes to write?
    mov     rdx, 14
  ; this instruction invokes a system call
    syscall
quit:
  mov rax, __NR_exit ; 60 exit
  mov rdi, 0         ; exit code
  syscall
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#---------------------------------------------------------
# Writes &quot;Hello, World&quot; to the console.
# To assemble and run:
# gcc -c hello.s &amp;&amp; ld hello.o &amp;&amp; ./a.out        or
# gcc -nostdlib hello.s &amp;&amp; ./a.out               or
# as -a=hello.lis --gstabs -o hello.o hello.s
# ld -o hello hello.o
#---------------------------------------------------------
    .include &quot;syscalls-att.inc&quot;
    
    .global _start
    .text
_start:
    # write(1, message, 13)
    mov $__NR_write, %rax  # system call code
    mov $1, %rdi           # file handle 1 is stdout
    mov $message, %rsi     # address of string to output
    mov $message_len, %rdx # number of bytes
    syscall
      # invoke operating system to do the write
    mov $__NR_exit, %rax # system call code
    xor %rdi, %rdi       # we want return code 0
    syscall              # invoke operating system to exit

    .data
message:
   .ascii &quot;Hello, world\n&quot;
   .equ   message_len, . - message
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
  .intel_syntax
  .global _start  # was global start

  .data           # was section .rodata
msg: .ascii  &quot;Hello, world!\n&quot;
     .equ msglen, . - msg

  .text            # was section.text
_start:
  mov %rax, 1      #; write(
  mov %rdi, 1      #; STDOUT_FILENO,
  lea %rsi, msg    #; &quot;Hello, world!\n&quot;,
  mov %rdx, msglen #; sizeof(&quot;Hello, world!\n&quot;)
  syscall          #; );
  mov %rax, 60     #; exit(
  mov %rdi, 0      #; EXIT_SUCCESS
  syscall          #; );
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 3</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
ASFLAGS= -a=$*.lis --gstabs

hello-intel : hello-intel.o
  ld -o hello-intel hello-intel.o
fib : fib.s
  gcc -ggdb -no-pie -o fib fib.s
hola : hola.s
  gcc -ggdb -no-pie -o hola hola.s
hello : hello.o
  ld -o hello hello.o
all : hello hola fib

clean:
  rm -f hello *.o *.lis
  rm -f printf hola fib hello-intel
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 4</td>
	</tr>
</table>

<h2>Debugging â€“ gdb</h2>

<p>I am no expert on gdb. I have spent a lot of energy just getting things to build and run properly. However, Iâ€™ve downloaded a copy of the <em>GDB Quick Reference</em> and pasted it into my assembly scrap book. You start the debugger with <code>gdb executable-name</code>. If gdb reports that it is â€˜reading symbolsâ€™, you have managed to create an executable with debug symbols available. You can also check this by typing <code>file executable-name</code> at a shell prompt. Once in the debugger, there are a whole load of commands available (see the available online documentation). Once in the debugger, I tend to set things up with these commands:</p>

<pre class="programlisting">
  break _start
  start
  layout src</pre>
  
<p>and then I use either the <code>step</code> command or the <code>next</code> commands to trace through the code.</p>

<h2>The future</h2>

<p>There is a free book available online â€“ <em>Intel 64-bit Assembly Language Programming with Ubuntu</em> <a href="#[7]">[7]</a> â€“ that I will be working through. And I will be using Google and accu-general for help as well.</p>

<h2>Thank you</h2>

<p>I would like to thank Tom Hughes, Bill Somerville, Jonathan Wakely and Ahtu Truu for their patience and help on accu-general.</p>

<h2>References</h2>

<p class="bibliomixed"><a id="[1]"></a>[1]	Ward, Brian (2014) <em>How Linux Works: What Every Superuser Should Know</em> (2nd ed.), No Starch Press, ISBN-13: 978-1593275679</p>

<p class="bibliomixed"><a id="[2]"></a>[2]	Shotts, William E. Jr. (2019) <em>The Linux Command Line: A Complete Introduction</em> (2nd ed.), No Starch Press,											ISBN-13: 978-1593279523</p>

<p class="bibliomixed"><a id="[3]"></a>[3]	Seyfarth, Ray (2014) <em>Introduction to 64 Bit Assembly Programming for Linux and OS X</em> (3rd ed.), CreateSpace Independent Publishing Platform, ISBN-13: 978-1484921906</p>

<p class="bibliomixed"><a id="[4]"></a>[4]	Zhirkov, Igor (2017) <em>Low-Level Programming: C, Assembly, and Program Execution on Intel 64 Architecture</em>, Apress, ISBN-13: 978-1484224021</p>

<p class="bibliomixed"><a id="[5]"></a>[5]	The source of the example, and also a learning resource:  <a href="http://cs.lmu.edu/~ray/notes/gasexamples/">http://cs.lmu.edu/~ray/notes/gasexamples/</a></p>

<p class="bibliomixed"><a id="[6]"></a>[6]	<a href="https://www.devdungeon.com/content/how-mix-c-and-assembly">https://www.devdungeon.com/content/how-mix-c-and-assembly</a></p>

<p class="bibliomixed"><a id="[7]"></a>[7]	<em>x86-64 Assembly Language Programming with Ubuntu</em> by Ed Jorgensen (2019), <a href="http://www.egr.unlv.edu/~ed/assembly64.pdf">http://www.egr.unlv.edu/~ed/assembly64.pdf</a></p>

<h3>Other resources used when learning assembly</h3>
<ul>
	<li>ABI for x64 architecture: <a href="http://refspecs.linuxbase.org/elf/index.html">http://refspecs.linuxbase.org/elf/index.html</a></li>
	
	<li>Assembly language manuals: <a href="https://software.intel.com/en-us/articles/intel-sdm">https://software.intel.com/en-us/articles/intel-sdm</a></li>
	
	<li>â€˜Bluff your way in x64 assemblerâ€™ by Roger Orr from <em>ACCU Conference 2017</em>, available on YouTube</li>
	
	<li>â€˜Enough x86 assembly to be dangerousâ€™ by Charles Bailey from <em>CPPCON 2017</em>, available on YouTube</li>
	
	<li>GNU gdb manual: <a href="https://www.gnu.org/software/gdb/documentation/">https://www.gnu.org/software/gdb/documentation/</a></li>
	
	<li>GNU toolchain manuals (make, as, ld): <a href="https://www.gnu.org/manual/manual.html">https://www.gnu.org/manual/manual.html</a></li>
	
	<li>Introduction to Assembly: <a href="https://software.intel.com/en-us/articles/introduction-to-x64-assembly">https://software.intel.com/en-us/articles/introduction-to-x64-assembly</a></li>
	
	<li>YASM manual: <a href="http://yasm.tortall.net/">http://yasm.tortall.net/</a></li>
</ul>

<p class="bio"><span class="author"><b>Ian Bruntlett</b></span> On and off, Ian has been programming for some years. He is a volunteer system administrator (among other things) for a mental health charity called Contact (<a href="www.contactmorpeth.org.uk">www.contactmorpeth.org.uk</a>). He is learning low-level and other, higher-level, aspects of programming.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
