CSC231 Mini Lab 1 Solution and Discussion
Revision as of 09:06, 12 September 2012 by Thiebaut (talk | contribs) (moved CSC231 Mini Lab 1 Solution & Discussion to CSC231 Mini Lab 1 Solution and Discussion)
--D. Thiebaut 09:38, 12 September 2012 (EDT)
Contents
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!)
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*
Could skel.asm be useful?
;;; 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