Assembly Language: Difference between revisions

From Rixort Wiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 5: Line 5:
* AT&T: Lots of $ and % symbols. Default for most disassembly tools on Linux.
* AT&T: Lots of $ and % symbols. Default for most disassembly tools on Linux.
* Intel: Cleaner and easier to read, requires the <code>-M intel</code> command line option to <code>objdump</code>.
* Intel: Cleaner and easier to read, requires the <code>-M intel</code> command line option to <code>objdump</code>.
AT&T and Intel have the operands in opposite orders.
If unsure, use Intel - if only because it will match the specifications (and the $ and % symbols make AT&T harder to read and more like Perl).


== Generating assembly from C ==
== Generating assembly from C ==
Line 49: Line 53:
* EDI: Destination Index
* EDI: Destination Index
* EIP: Instruction Point
* EIP: Instruction Point
== Makefile ==
Skeleton Makefile to assemble and link a source file into an executable
%: %.o
    gcc -o $@ $<
%.o: %.s
    as -o $@ $<
== Articles ==
* [https://www.timdbg.com/posts/fakers-guide-to-assembly/ The faker's guide to reading (x86) assembly language]


== Links ==
== Links ==
Line 56: Line 74:
* [http://www.bravegnu.org/gnu-eprog/ Embedded Programming with the GNU Toolchain]
* [http://www.bravegnu.org/gnu-eprog/ Embedded Programming with the GNU Toolchain]
* [https://azeria-labs.com/writing-arm-assembly-part-1/ Writing ARM Assembly]
* [https://azeria-labs.com/writing-arm-assembly-part-1/ Writing ARM Assembly]
* [https://thinkingeek.com/arm-assembler-raspberry-pi/ ARM assembler in Raspberry Pi]
* [https://docs.google.com/document/d/e/2PACX-1vRs5uAkVzrUqeyKDSzxw0VofIJDCQd_TLFtE36HmYFFPQiIotV-T3PHo1Js9eNTbGayaaJ5dZTNCifl/pub CS301 Assembly Language Programming - Fall 2022]


[[Category:Programming]]
[[Category:Programming]]

Latest revision as of 16:34, 10 January 2023

Syntax

Two main x86 assembly syntax:

  • AT&T: Lots of $ and % symbols. Default for most disassembly tools on Linux.
  • Intel: Cleaner and easier to read, requires the -M intel command line option to objdump.

AT&T and Intel have the operands in opposite orders.

If unsure, use Intel - if only because it will match the specifications (and the $ and % symbols make AT&T harder to read and more like Perl).

Generating assembly from C

Generating assembly: Use the -S flag in gcc or clang, e.g. gcc -S foo.c produces foo.s.

Comparison expression in C, such as i < 10 (in a for loop), appear to be translated to different assembly by different compilers, and not necessarily what you would expect.

GCC:

cmpl	$9, -4(%rbp)
jle	.L3

'if i <= 9, jump to the beginning of the loop'.

clang:

cmpl	$10, -8(%rbp)
jge	.LBB0_4

'if i >= 10, jump to the code block after the loop'.

Neither compiler compares the integer 10 with the contents of i. Although the approaches taken by the compilers are functionally identical, it is interesting that they have both decided to generate assembly with different logic to the original code, yet in two different ways.

Furthermore, in both cases the compiler does not compile the i++ statement within the for loop to an inc command, contrary to what most C programming books would claim. Possibly the case that the difference between inc and add is non-existent on modern processors, or that the processor itself will automatically treat inc (source) and add $1, (source) as the same.

Registers

16-bit registers: AX, BX, CX, DX, FLAGS

32-bit registers: EAX (Extended AX), EFLAGS etc.

64-bit registers: RAX etc.

Named registers:

  • EAX: Accumulator
  • EBX: Base
  • ECX: Counter
  • EDX: Data
  • ESP: Stack Pointer
  • EBP: Base Pointer
  • ESI: Source Index
  • EDI: Destination Index
  • EIP: Instruction Point

Makefile

Skeleton Makefile to assemble and link a source file into an executable

%: %.o
    gcc -o $@ $<

%.o: %.s
    as -o $@ $<

Articles

Links