CSC231 Mini Lab 1 Solution and Discussion
--D. Thiebaut 09:38, 12 September 2012 (EDT)
Contents
This is a presentation of two solutions to the Mini Lab #1 and a discussion of the two different approaches.
Solution 1
;;; minilab1_v1.asm
;;; D. Thiebaut
;;; prints this string on the screen:
;;;
;;; *********************************
;;; * Welcome to CSC231 *
;;; * Home of the Assembly Language *
;;; *********************************
section .data
msg db "*********************************", 10
db "* Welcome to CSC231 *", 10
db "* Home of the Assembly Language *", 10
db "*********************************", 10, 10
msgLen equ $-msg
section .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,msgLen
int 0x80
;;; exit
mov eax,1
mov ebx,0
int 0x80
Solution 2
;;; minilab1_v2.asm
;;; D. Thiebaut
;;; prints this string on the screen:
;;;
;;; *********************************
;;; * Welcome to CSC231 *
;;; * Home of the Assembly Language *
;;; *********************************
section .data
msg db "*********************************", 10
msgLen1 equ $-msg
db "* Welcome to CSC231 *", 10
db "* Home of the Assembly Language *", 10
msgLen2 equ $-msg
section .text
global _start
_start:
;;; print first 3 lines
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,msgLen2
int 0x80
;;; print first line back, to close box
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,msgLen1
int 0x80
;;; exit
mov eax,1
mov ebx,0
int 0x80
Understanding what's inside the executable
- Generate the listing of the assembly program:
nasm -f elf -F stabs -l minilab1_v1.lst minilab1_v1.asm (that's a minus ell option in the command)
- Look at the new files in your account:
ls -l minilab1_v1.* -rwx------. 1 231a 231a 1079 Sep 12 09:18 minilab1_v1* -rw-------. 1 231a 231a 606 Sep 12 09:18 minilab1_v1.asm -rw-------. 1 231a 231a 2398 Sep 12 09:47 minilab1_v1.lst -rw-------. 1 231a 231a 1168 Sep 12 09:47 minilab1_v1.o
- Look at the listing file generated by the -l switch
1 ;;; minilab1.asm
2 ;;; D. Thiebaut
3 ;;; prints this string on the screen:
4 ;;;
5 ;;; *********************************
6 ;;; * Welcome to CSC231 *
7 ;;; * Home of the Assembly Language *
8 ;;; *********************************
9
10
11 section .data
12
13 00000000 2A2A2A2A2A2A2A2A2A- msg db "*********************************", 10
14 00000009 2A2A2A2A2A2A2A2A2A-
15 00000012 2A2A2A2A2A2A2A2A2A-
16 0000001B 2A2A2A2A2A2A0A
17 00000022 2A2057656C636F6D65- db "* Welcome to CSC231 *", 10
18 0000002B 20746F204353433233-
19 00000034 312020202020202020-
20 0000003D 20202020202A0A
21 00000044 2A20486F6D65206F66- db "* Home of the Assembly Language *", 10
22 0000004D 207468652041737365-
23 00000056 6D626C79204C616E67-
24 0000005F 75616765202A0A
25 00000066 2A2A2A2A2A2A2A2A2A- db "*********************************", 10, 10
26 0000006F 2A2A2A2A2A2A2A2A2A-
27 00000078 2A2A2A2A2A2A2A2A2A-
28 00000081 2A2A2A2A2A2A0A0A
29
30 msgLen equ $-msg
31
32 section .text
33 global _start
34 _start:
35 00000000 B804000000 mov eax,4
36 00000005 BB01000000 mov ebx,1
37 0000000A B9[00000000] mov ecx,msg
38 0000000F BA89000000 mov edx,msgLen
39 00000014 CD80 int 0x80
40
41 ;;; exit
42 00000016 B801000000 mov eax,1
43 0000001B BB00000000 mov ebx,0
44 00000020 CD80 int 0x80
45
46
- Try to recognize all the features. What is code, what is data, what strings like "CD80" mean... (we'll do that in class!)
- A quick review of hexadecimal: tutorial from electronics-tutorials.ws
How does this relate to the size of the executable?
-rw-r--r--. 1 231a 231a 324 Sep 12 10:04 skel.asm -rw-r--r--. 1 231a 231a 780 Sep 12 10:04 skel2.asm -rw-------. 1 231a 231a 707 Sep 12 10:04 minilab1_v2.asm -rw-------. 1 231a 231a 606 Sep 12 10:04 minilab1_v1.asm -rw-------. 1 231a 231a 606 Sep 12 10:04 minilab1.asm -rw-r--r--. 1 231a 231a 996 Sep 12 10:04 lab1.asm -rw-r--r--. 1 231a 231a 556 Sep 12 10:04 hello.asm -rw-r--r--. 1 231a 231a 208 Sep 12 10:04 hello3.asm -rw-r--r--. 1 231a 231a 501 Sep 12 10:04 hello2.asm -rw-------. 1 231a 231a 752 Sep 12 10:05 skel.o -rw-------. 1 231a 231a 1248 Sep 12 10:05 minilab1_v2.o -rw-------. 1 231a 231a 1168 Sep 12 10:05 minilab1_v1.o -rw-------. 1 231a 231a 1136 Sep 12 10:05 minilab1.o -rw-r--r--. 1 231a 231a 1024 Sep 12 10:05 lab1.o -rw-r--r--. 1 231a 231a 1024 Sep 12 10:05 hello.o -rwx------. 1 231a 231a 705 Sep 12 10:05 skel* -rwx------. 1 231a 231a 1148 Sep 12 10:05 minilab1_v2* -rwx------. 1 231a 231a 1079 Sep 12 10:05 minilab1_v1*
The Hexdump utility
- Hexdump is a Linux utility that will take any file on your disk and dump its byte contents to the screen in hexadecimal and Ascii format (it supports many options to display the bytes in various formats; hexadecimal and Ascii is fine for us).
- Here is the output of the command hexdump -C minilab1_v1:
hexdump -C minilab1_v1 00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............| 00000010 02 00 03 00 01 00 00 00 80 80 04 08 34 00 00 00 |............4...| 00000020 fc 01 00 00 00 00 00 00 34 00 20 00 02 00 28 00 |........4. ...(.| 00000030 08 00 05 00 01 00 00 00 00 00 00 00 00 80 04 08 |................| 00000040 00 80 04 08 a2 00 00 00 a2 00 00 00 05 00 00 00 |................| 00000050 00 10 00 00 01 00 00 00 a4 00 00 00 a4 90 04 08 |................| 00000060 a4 90 04 08 89 00 00 00 89 00 00 00 06 00 00 00 |................| 00000070 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000080 b8 04 00 00 00 bb 01 00 00 00 b9 a4 90 04 08 ba |................| 00000090 89 00 00 00 cd 80 b8 01 00 00 00 bb 00 00 00 00 |................| 000000a0 cd 80 00 00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |....************| 000000b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |****************| 000000c0 2a 2a 2a 2a 2a 0a 2a 20 57 65 6c 63 6f 6d 65 20 |*****.* Welcome | 000000d0 74 6f 20 43 53 43 32 33 31 20 20 20 20 20 20 20 |to CSC231 | 000000e0 20 20 20 20 20 20 2a 0a 2a 20 48 6f 6d 65 20 6f | *.* Home o| 000000f0 66 20 74 68 65 20 41 73 73 65 6d 62 6c 79 20 4c |f the Assembly L| 00000100 61 6e 67 75 61 67 65 20 2a 0a 2a 2a 2a 2a 2a 2a |anguage *.******| 00000110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |****************| 00000120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 0a 00 00 00 |***********.....| 00000130 01 00 00 00 00 00 0a 00 11 00 00 00 01 00 00 00 |................| 00000140 64 00 00 00 80 80 04 08 00 00 00 00 44 00 17 00 |d...........D...| 00000150 80 80 04 08 00 00 00 00 44 00 18 00 85 80 04 08 |........D.......| 00000160 00 00 00 00 44 00 19 00 8a 80 04 08 00 00 00 00 |....D...........| 00000170 44 00 1a 00 8f 80 04 08 00 00 00 00 44 00 1b 00 |D...........D...| 00000180 94 80 04 08 00 00 00 00 44 00 1e 00 96 80 04 08 |........D.......| 00000190 00 00 00 00 44 00 1f 00 9b 80 04 08 00 00 00 00 |....D...........| 000001a0 44 00 20 00 a0 80 04 08 00 00 00 00 64 00 00 00 |D. .........d...| 000001b0 00 00 00 00 00 6d 69 6e 69 6c 61 62 31 5f 76 31 |.....minilab1_v1| 000001c0 2e 61 73 6d 00 00 2e 73 79 6d 74 61 62 00 2e 73 |.asm...symtab..s| 000001d0 74 72 74 61 62 00 2e 73 68 73 74 72 74 61 62 00 |trtab..shstrtab.| 000001e0 2e 74 65 78 74 00 2e 64 61 74 61 00 2e 73 74 61 |.text..data..sta| 000001f0 62 00 2e 73 74 61 62 73 74 72 00 00 00 00 00 00 |b..stabstr......| 00000200 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00000220 00 00 00 00 1b 00 00 00 01 00 00 00 06 00 00 00 |................| 00000230 80 80 04 08 80 00 00 00 22 00 00 00 00 00 00 00 |........".......| 00000240 00 00 00 00 10 00 00 00 00 00 00 00 21 00 00 00 |............!...| 00000250 01 00 00 00 03 00 00 00 a4 90 04 08 a4 00 00 00 |................| 00000260 89 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 |................| 00000270 00 00 00 00 27 00 00 00 01 00 00 00 00 00 00 00 |....'...........| 00000280 00 00 00 00 30 01 00 00 84 00 00 00 04 00 00 00 |....0...........| 00000290 00 00 00 00 04 00 00 00 0c 00 00 00 2d 00 00 00 |............-...| 000002a0 03 00 00 00 00 00 00 00 00 00 00 00 b4 01 00 00 |................| 000002b0 11 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 |................| 000002c0 00 00 00 00 11 00 00 00 03 00 00 00 00 00 00 00 |................| 000002d0 00 00 00 00 c5 01 00 00 36 00 00 00 00 00 00 00 |........6.......| 000002e0 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 |................| 000002f0 02 00 00 00 00 00 00 00 00 00 00 00 3c 03 00 00 |............<...| 00000300 c0 00 00 00 07 00 00 00 08 00 00 00 04 00 00 00 |................| 00000310 10 00 00 00 09 00 00 00 03 00 00 00 00 00 00 00 |................| 00000320 00 00 00 00 fc 03 00 00 3b 00 00 00 00 00 00 00 |........;.......| 00000330 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 |................| 00000340 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000350 80 80 04 08 00 00 00 00 03 00 01 00 00 00 00 00 |................| 00000360 a4 90 04 08 00 00 00 00 03 00 02 00 00 00 00 00 |................| 00000370 00 00 00 00 00 00 00 00 03 00 03 00 00 00 00 00 |................| 00000380 00 00 00 00 00 00 00 00 03 00 04 00 01 00 00 00 |................| 00000390 00 00 00 00 00 00 00 00 04 00 f1 ff 11 00 00 00 |................| 000003a0 a4 90 04 08 01 00 00 00 01 00 02 00 15 00 00 00 |................| 000003b0 89 00 00 00 00 00 00 00 00 00 f1 ff 1c 00 00 00 |................| 000003c0 80 80 04 08 00 00 00 00 10 00 01 00 23 00 00 00 |............#...| 000003d0 2d 91 04 08 00 00 00 00 10 00 f1 ff 2f 00 00 00 |-.........../...| 000003e0 2d 91 04 08 00 00 00 00 10 00 f1 ff 36 00 00 00 |-...........6...| 000003f0 30 91 04 08 00 00 00 00 10 00 f1 ff 00 6d 69 6e |0............min| 00000400 69 6c 61 62 31 5f 76 31 2e 61 73 6d 00 6d 73 67 |ilab1_v1.asm.msg| 00000410 00 6d 73 67 4c 65 6e 00 5f 73 74 61 72 74 00 5f |.msgLen._start._| 00000420 5f 62 73 73 5f 73 74 61 72 74 00 5f 65 64 61 74 |_bss_start._edat| 00000430 61 00 5f 65 6e 64 00 |a._end.| 00000437
Could skel.asm be useful?
- More precisely, could skel.asm be useful to compare different assembly-language programs in terms of their length?
;;; skel.asm
;;; D. Thiebaut
;;;
;;;
;;;
;;; To assemble, link, and run:
;;; nasm -f elf -F stabs skel.asm
;;; ld -melf_i386 -o skel skel.o
;;; ./skel
;;;
section .data
;;; put your variables here...
section .text
global _start
_start:
;;; put your code here
;;; exit
mov ebx, 0
mov eax, 1
int 0x80