SPO 600 Lab 3 – Assembler Lab (Part 1)

1) Build and run the three C versions of the program for x86_64

2) Use the objdump -d command to print the object code and disassemble it into assembler for each of the binaries

printf("Hello World!\n");
0000000000401126 <main>:
  401126:	55                   	push   %rbp
  401127:	48 89 e5             	mov    %rsp,%rbp
  40112a:	bf 10 20 40 00       	mov    $0x402010,%edi
  40112f:	e8 fc fe ff ff       	callq  401030 <puts@plt>
  401134:	b8 00 00 00 00       	mov    $0x0,%eax
  401139:	5d                   	pop    %rbp
  40113a:	c3                   	retq   
  40113b:	0f 1f 44 00 00       	nopl   0x0(%rax,%rax,1)
write(1,"Hello World!\n",13);
0000000000401126 <main>:
  401126:	55                   	push   %rbp
  401127:	48 89 e5             	mov    %rsp,%rbp
  40112a:	ba 0d 00 00 00       	mov    $0xd,%edx
  40112f:	be 10 20 40 00       	mov    $0x402010,%esi
  401134:	bf 01 00 00 00       	mov    $0x1,%edi
  401139:	e8 f2 fe ff ff       	callq  401030 <write@plt>
  40113e:	b8 00 00 00 00       	mov    $0x0,%eax
  401143:	5d                   	pop    %rbp
  401144:	c3                   	retq   
  401145:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
  40114c:	00 00 00 
  40114f:	90                   	nop

syscall(__NR_write,1,"Hello World!\n",13);
0000000000401126 <main>:
  401126:	55                   	push   %rbp
  401127:	48 89 e5             	mov    %rsp,%rbp
  40112a:	b9 0d 00 00 00       	mov    $0xd,%ecx
  40112f:	ba 10 20 40 00       	mov    $0x402010,%edx
  401134:	be 01 00 00 00       	mov    $0x1,%esi
  401139:	bf 01 00 00 00       	mov    $0x1,%edi
  40113e:	b8 00 00 00 00       	mov    $0x0,%eax
  401143:	e8 e8 fe ff ff       	callq  401030 <syscall@plt>
  401148:	b8 00 00 00 00       	mov    $0x0,%eax
  40114d:	5d                   	pop    %rbp
  40114e:	c3                   	retq   
  40114f:	90                   	nop

3) Review, build and run the x86_64 assembly language programs

[cle@xerxes x86_64]$ cat hello-gas.s
/* 
   This is a 'hello world' program in x86_64 assembler using the 
   GNU assembler (gas) syntax. Note that this program runs in 64-bit
   mode.

   CTyler, Seneca College, 2014-01-20
   Licensed under GNU GPL v2+
*/

.text
.globl	_start

_start:
	movq	$len,%rdx			/* message length */
	movq 	$msg,%rsi			/* message location */
	movq	$1,%rdi				/* file descriptor stdout */
	movq	$1,%rax				/* syscall sys_write */
	syscall

	movq	$0,%rdi				/* exit status */
	movq	$60,%rax			/* syscall sys_exit */
	syscall

.section .rodata

msg:	.ascii      "Hello, world!\n"
	len = . - msg
[cle@xerxes x86_64]$ as -g -o hello-gas.o hello-gas.s
[cle@xerxes x86_64]$ ld -o hello-gas hello-gas.o
[cle@xerxes x86_64]$ ./hello-gas
Hello, world!
[cle@xerxes x86_64]$ objdump -d hello-gas

hello-gas:     file format elf64-x86-64


Disassembly of section .text:

0000000000401000 <_start>:
  401000:	48 c7 c2 0e 00 00 00 	mov    $0xe,%rdx
  401007:	48 c7 c6 00 20 40 00 	mov    $0x402000,%rsi
  40100e:	48 c7 c7 01 00 00 00 	mov    $0x1,%rdi
  401015:	48 c7 c0 01 00 00 00 	mov    $0x1,%rax
  40101c:	0f 05                	syscall 
  40101e:	48 c7 c7 00 00 00 00 	mov    $0x0,%rdi
  401025:	48 c7 c0 3c 00 00 00 	mov    $0x3c,%rax
  40102c:	0f 05                	syscall 
[cle@xerxes x86_64]$ cat hello-nasm.s
section	.text
global	_start

_start:
	mov	rdx,len			; message length
	mov	rcx,msg			; message location
	mov	rbx,1			; file descriptor stdout
	mov	rax,4			; syscall sys_write
	int	0x80

	mov	rax,1			; syscall sys_exit
	int	0x80

section	.rodata

msg	db	'Hello, world!',0xa
len	equ	$ - msg
[cle@xerxes x86_64]$ nasm -g -f elf64 -o hello-nasm.o hello-nasm.s
[cle@xerxes x86_64]$ ld -o hello-nasm hello-nasm.o
[cle@xerxes x86_64]$ ./hello-nasm
Hello, world!
[cle@xerxes x86_64]$ objdump -d hello-nasm
hello-nasm:     file format elf64-x86-64


Disassembly of section .text:

0000000000401000 <_start>:
  401000:	ba 0e 00 00 00       	mov    $0xe,%edx
  401005:	48 b9 00 20 40 00 00 	movabs $0x402000,%rcx
  40100c:	00 00 00 
  40100f:	bb 01 00 00 00       	mov    $0x1,%ebx
  401014:	b8 04 00 00 00       	mov    $0x4,%eax
  401019:	cd 80                	int    $0x80
  40101b:	b8 01 00 00 00       	mov    $0x1,%eax
  401020:	cd 80                	int    $0x80

4) Build and run the three C versions of the program for aarch64

printf("Hello World!\n");
0000000000400594 <main>:
  400594:	a9bf7bfd 	stp	x29, x30, [sp, #-16]!
  400598:	910003fd 	mov	x29, sp
  40059c:	90000000 	adrp	x0, 400000 <_init-0x418>
  4005a0:	9119c000 	add	x0, x0, #0x670
  4005a4:	97ffffb7 	bl	400480 <puts@plt>
  4005a8:	52800000 	mov	w0, #0x0                   	// #0
  4005ac:	a8c17bfd 	ldp	x29, x30, [sp], #16
  4005b0:	d65f03c0 	ret
  4005b4:	00000000 	.inst	0x00000000 ; undefined
write(1,"Hello World!\n",13);
0000000000400594 <main>:
  400594:	a9bf7bfd 	stp	x29, x30, [sp, #-16]!
  400598:	910003fd 	mov	x29, sp
  40059c:	d28001a2 	mov	x2, #0xd                   	// #13
  4005a0:	90000000 	adrp	x0, 400000 <_init-0x418>
  4005a4:	9119e001 	add	x1, x0, #0x678
  4005a8:	52800020 	mov	w0, #0x1                   	// #1
  4005ac:	97ffffb1 	bl	400470 <write@plt>
  4005b0:	52800000 	mov	w0, #0x0                   	// #0
  4005b4:	a8c17bfd 	ldp	x29, x30, [sp], #16
  4005b8:	d65f03c0 	ret
  4005bc:	00000000 	.inst	0x00000000 ; undefined
syscall(__NR_write,1,"Hello World!\n",13);
0000000000400594 <main>:
  400594:	a9bf7bfd 	stp	x29, x30, [sp, #-16]!
  400598:	910003fd 	mov	x29, sp
  40059c:	528001a3 	mov	w3, #0xd                   	// #13
  4005a0:	90000000 	adrp	x0, 400000 <_init-0x418>
  4005a4:	9119e002 	add	x2, x0, #0x678
  4005a8:	52800021 	mov	w1, #0x1                   	// #1
  4005ac:	d2800800 	mov	x0, #0x40                  	// #64
  4005b0:	97ffffb4 	bl	400480 <syscall@plt>
  4005b4:	52800000 	mov	w0, #0x0                   	// #0
  4005b8:	a8c17bfd 	ldp	x29, x30, [sp], #16
  4005bc:	d65f03c0 	ret

5) Review, build and run the aarch64 assembly language programs

[cle@aarchie aarch64]$ cat hello.s
.text
.globl _start
_start:
 
	mov     x0, 1           /* file descriptor: 1 is stdout */
	adr     x1, msg   	/* message location (memory address) */
	mov     x2, len   	/* message length (bytes) */

	mov     x8, 64     	/* write is syscall #64 */
	svc     0          	/* invoke syscall */
 
	mov     x0, 0     	/* status -> 0 */
	mov     x8, 93    	/* exit is syscall #93 */
	svc     0          	/* invoke syscall */
 
.data
msg: 	.ascii      "Hello, world!\n"
len= 	. - msg
[cle@aarchie aarch64]$ as -g -o hello.o hello.s
[cle@aarchie aarch64]$ ld -o hello hello.o
[cle@aarchie aarch64]$ ./hello
Hello, world!
[cle@aarchie aarch64]$ objdump -d hello

hello:     file format elf64-littleaarch64


Disassembly of section .text:

00000000004000b0 <_start>:
  4000b0:	d2800020 	mov	x0, #0x1                   	// #1
  4000b4:	100800e1 	adr	x1, 4100d0 <msg>
  4000b8:	d28001c2 	mov	x2, #0xe                   	// #14
  4000bc:	d2800808 	mov	x8, #0x40                  	// #64
  4000c0:	d4000001 	svc	#0x0
  4000c4:	d2800000 	mov	x0, #0x0                   	// #0
  4000c8:	d2800ba8 	mov	x8, #0x5d                  	// #93
  4000cc:	d4000001 	svc	#0x0

6) Looping in x86_64 https://github.com/cindyledev/Software-Portability-and-Optimization/blob/master/Lab%203/basic_loop.s

Looping with 0-9 https://github.com/cindyledev/Software-Portability-and-Optimization/blob/master/Lab%203/loop_counter.s

Continued here

Published by cindyledev

Full Stack Developer, Computer Programmer, and Analyst

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: