Difference between revisions of "CSC231 Copying Strings in RAM"

From dftwiki3
Jump to: navigation, search
(One Word at a Time)
(A Program with 2 Strings)
Line 4: Line 4:
 
=A Program with 2 Strings=
 
=A Program with 2 Strings=
  
 +
We are going to use this simple program as our starting point for copying all the characters of msg1 into msg2.
 
<source lang="asm">
 
<source lang="asm">
 
;;; movString1s.asm
 
;;; movString1s.asm
Line 45: Line 46:
 
<br />
 
<br />
 
<br />
 
<br />
 +
 
=Copying msg1 into msg2, 1 byte at a time=
 
=Copying msg1 into msg2, 1 byte at a time=
 
(Remember, we don't know loops yet!)
 
(Remember, we don't know loops yet!)

Revision as of 08:31, 19 September 2012

--D. Thiebaut 09:08, 19 September 2012 (EDT)


A Program with 2 Strings

We are going to use this simple program as our starting point for copying all the characters of msg1 into msg2.

;;; movString1s.asm
;;; D. Thiebaut
;;;
;;; 
;;;
;;; To assemble, link, and run:
;;; 	nasm -f elf -F stabs movString1s.asm
;;; 	ld -melf_i386 -o movStrings1 movStrings1.o
;;; 	./movStrings1
;;;

		section	.data
msg1		db	"Smith College", 10
msg1Len		equ	$-msg1
msg2		db	".............", 10
	
		section	.text
		global	_start
_start:	

;;; print msg1
		mov	eax, 4
		mov	ebx, 1
		mov 	ecx, msg1
		mov	edx, msg1Len
		int	0x80

;;; print msg2
		mov	eax, 4
		mov	ebx, 1
		mov 	ecx, msg2
		mov	edx, msg1Len
		int	0x80
;;; exit
		mov	ebx, 0
		mov	eax, 1
		int	0x80



Copying msg1 into msg2, 1 byte at a time

(Remember, we don't know loops yet!)

     1                                  ;;; movString1s.asm
     2                                  ;;; D. Thiebaut
     3                                  ;;;
     4                                  ;;; This version copies msg1 to msg2
     5                                  ;;;
     6                                  ;;; To assemble, link, and run:
     7                                  ;;; 	nasm -f elf -F stabs movString1s.asm
     8                                  ;;; 	ld -melf_i386 -o movStrings2 movStrings2.o
     9                                  ;;; 	./movStrings2
    10                                  ;;;
    11                                  
    12                                  		section	.data
    13 00000000 536D69746820436F6C-     msg1		db	"Smith College", 10
    14 00000009 6C6567650A         
    15                                  msg1Len		equ	$-msg1
    16 0000000E 2E2E2E2E2E2E2E2E2E-     msg2		db	".............", 10
    17 00000017 2E2E2E2E0A         
    18                                  	
    19                                  		section	.text
    20                                  		global	_start
    21                                  _start:	
    22                                  
    23                                  ;;; print msg1
    24 00000000 B804000000              		mov	eax, 4
    25 00000005 BB01000000              		mov	ebx, 1
    26 0000000A B9[00000000]            		mov 	ecx, msg1
    27 0000000F BA0E000000              		mov	edx, msg1Len
    28 00000014 CD80                    		int	0x80
    29                                  
    30                                  ;;; print original msg2
    31 00000016 B804000000              		mov	eax, 4
    32 0000001B BB01000000              		mov	ebx, 1
    33 00000020 B9[0E000000]            		mov 	ecx, msg2
    34 00000025 BA0E000000              		mov	edx, msg1Len
    35 0000002A CD80                    		int	0x80
    36                                  
    37                                  ;;; copy msg1 into msg2
    38 0000002C A0[00000000]            		mov	al, byte [msg1]
    39 00000031 A2[0E000000]            		mov	byte[msg2], al
    40                                  
    41 00000036 A0[01000000]            		mov	al, byte [msg1+1]
    42 0000003B A2[0F000000]            		mov	byte[msg2+1], al
    43                                  
    44 00000040 A0[02000000]            		mov	al, byte [msg1+2]
    45 00000045 A2[10000000]            		mov	byte[msg2+2], al
    46                                  
    47 0000004A A0[03000000]            		mov	al, byte [msg1+3]
    48 0000004F A2[11000000]            		mov	byte[msg2+3], al
    49                                  
    50 00000054 A0[04000000]            		mov	al, byte [msg1+4]
    51 00000059 A2[12000000]            		mov	byte[msg2+4], al
    52                                  
    53 0000005E A0[05000000]            		mov	al, byte [msg1+5]
    54 00000063 A2[13000000]            		mov	byte[msg2+5], al
    55                                  
    56 00000068 A0[06000000]            		mov	al, byte [msg1+6]
    57 0000006D A2[14000000]            		mov	byte[msg2+6], al
    58                                  
    59 00000072 A0[07000000]            		mov	al, byte [msg1+7]
    60 00000077 A2[15000000]            		mov	byte[msg2+7], al
    61                                  
    62 0000007C A0[08000000]            		mov	al, byte [msg1+8]
    63 00000081 A2[16000000]            		mov	byte[msg2+8], al
    64                                  
    65 00000086 A0[09000000]            		mov	al, byte [msg1+9]
    66 0000008B A2[17000000]            		mov	byte[msg2+9], al
    67                                  
    68 00000090 A0[0A000000]            		mov	al, byte [msg1+10]
    69 00000095 A2[18000000]            		mov	byte[msg2+10], al
    70                                  
    71 0000009A A0[0B000000]            		mov	al, byte [msg1+11]
    72 0000009F A2[19000000]            		mov	byte[msg2+11], al
    73                                  
    74 000000A4 A0[0C000000]            		mov	al, byte [msg1+12]
    75 000000A9 A2[1A000000]            		mov	byte[msg2+12], al
    76                                  
    77                                  ;;; print new msg2
    78 000000AE B804000000              		mov	eax, 4
    79 000000B3 BB01000000              		mov	ebx, 1
    80 000000B8 B9[0E000000]            		mov 	ecx, msg2
    81 000000BD BA0E000000              		mov	edx, msg1Len
    82 000000C2 CD80                    		int	0x80
    83                                  
    84                                  ;;; exit
    85 000000C4 BB00000000              		mov	ebx, 0
    86 000000C9 B801000000              		mov	eax, 1
    87 000000CE CD80                    		int	0x80
    88                                  
    89



One Word at a Time


     1                                  ;;; movStrings3.asm
     2                                  ;;; D. Thiebaut
     3                                  ;;;
     4                                  ;;; This version copies msg1 to msg2, one word at a time...
     5                                  ;;;
     6                                  ;;; To assemble, link, and run:
     7                                  ;;; 	nasm -f elf -F stabs movString1s.asm
     8                                  ;;; 	ld -melf_i386 -o movStrings3 movStrings3.o
     9                                  ;;; 	./movStrings3
    10                                  ;;;
    11                                  
    12                                  		section	.data
    13 00000000 536D69746820436F6C-     msg1		db	"Smith College", 10
    14 00000009 6C6567650A         
    15                                  msg1Len		equ	$-msg1
    16 0000000E 2E2E2E2E2E2E2E2E2E-     msg2		db	".............", 10
    17 00000017 2E2E2E2E0A         
    18                                  	
    19                                  		section	.text
    20                                  		global	_start
    21                                  _start:	
    22                                  
    23                                  ;;; print msg1
    24 00000000 B804000000              		mov	eax, 4
    25 00000005 BB01000000              		mov	ebx, 1
    26 0000000A B9[00000000]            		mov 	ecx, msg1
    27 0000000F BA0E000000              		mov	edx, msg1Len
    28 00000014 CD80                    		int	0x80
    29                                  
    30                                  ;;; print original msg2
    31 00000016 B804000000              		mov	eax, 4
    32 0000001B BB01000000              		mov	ebx, 1
    33 00000020 B9[0E000000]            		mov 	ecx, msg2
    34 00000025 BA0E000000              		mov	edx, msg1Len
    35 0000002A CD80                    		int	0x80
    36                                  
    37                                  ;;; copy msg1 into msg2
    38 0000002C 66A1[00000000]          		mov	ax, word [msg1]
    39 00000032 66A3[0E000000]          		mov	word[msg2], ax
    40                                  
    41 00000038 66A1[02000000]          		mov	ax, word [msg1+2]
    42 0000003E 66A3[10000000]          		mov	word[msg2+2], ax
    43                                  	
    44 00000044 66A1[04000000]          		mov	ax, word [msg1+4]
    45 0000004A 66A3[12000000]          		mov	word[msg2+4], ax
    46                                  	
    47 00000050 66A1[06000000]          		mov	ax, word [msg1+6]
    48 00000056 66A3[14000000]          		mov	word[msg2+6], ax
    49                                  	
    50 0000005C 66A1[08000000]          		mov	ax, word [msg1+8]
    51 00000062 66A3[16000000]          		mov	word[msg2+8], ax
    52                                  	
    53 00000068 66A1[0A000000]          		mov	ax, word [msg1+10]
    54 0000006E 66A3[18000000]          		mov	word[msg2+10], ax
    55                                  
    56 00000074 66A1[0C000000]          		mov	ax, word [msg1+12]
    57 0000007A 66A3[1A000000]          		mov	word[msg2+12], ax
    58                                  
    59                                  
    60                                  
    61                                  ;;; print new msg2
    62 00000080 B804000000              		mov	eax, 4
    63 00000085 BB01000000              		mov	ebx, 1
    64 0000008A B9[0E000000]            		mov 	ecx, msg2
    65 0000008F BA0E000000              		mov	edx, msg1Len
    66 00000094 CD80                    		int	0x80
    67                                  
    68                                  ;;; exit
    69 00000096 BB00000000              		mov	ebx, 0
    70 0000009B B801000000              		mov	eax, 1
    71 000000A0 CD80                    		int	0x80
    72                                  
    73



One Double-Word at a Time


     1                                  ;;; movStrings4.asm
     2                                  ;;; D. Thiebaut
     3                                  ;;;
     4                                  ;;; This version copies msg1 to msg2, one doubleword at a time...
     5                                  ;;;
     6                                  ;;; To assemble, link, and run:
     7                                  ;;; 	nasm -f elf -F stabs movString1s.asm
     8                                  ;;; 	ld -melf_i386 -o movStrings4 movStrings4.o
     9                                  ;;; 	./movStrings4
    10                                  ;;;
    11                                  ;;; ------------------------------------------------------------------
    12                                  ;;; WARNING!  This program has a bug!  Can you figure out where it is?
    13                                  ;;; Even better, can you expose the bug, as the output of this program
    14                                  ;;; seems correct?
    15                                  ;;; Here's the output:
    16                                  ;;; 
    17                                  ;;; Smith College
    18                                  ;;; .............
    19                                  ;;; Smith College
    20                                  ;;; 
    21                                  ;;; ------------------------------------------------------------------
    22                                  	
    23                                  
    24                                  		section	.data
    25 00000000 536D69746820436F6C-     msg1		db	"Smith College", 10
    26 00000009 6C6567650A         
    27                                  msg1Len		equ	$-msg1
    28 0000000E 2E2E2E2E2E2E2E2E2E-     msg2		db	".............", 10
    29 00000017 2E2E2E2E0A         
    30                                  	
    31                                  		section	.text
    32                                  		global	_start
    33                                  _start:	
    34                                  
    35                                  ;;; print msg1
    36 00000000 B804000000              		mov	eax, 4
    37 00000005 BB01000000              		mov	ebx, 1
    38 0000000A B9[00000000]            		mov 	ecx, msg1
    39 0000000F BA0E000000              		mov	edx, msg1Len
    40 00000014 CD80                    		int	0x80
    41                                  
    42                                  ;;; print original msg2
    43 00000016 B804000000              		mov	eax, 4
    44 0000001B BB01000000              		mov	ebx, 1
    45 00000020 B9[0E000000]            		mov 	ecx, msg2
    46 00000025 BA0E000000              		mov	edx, msg1Len
    47 0000002A CD80                    		int	0x80
    48                                  
    49                                  ;;; copy msg1 into msg2
    50 0000002C A1[00000000]            		mov	eax, dword [msg1]
    51 00000031 A3[0E000000]            		mov	dword[msg2], eax
    52                                  
    53 00000036 A1[04000000]            		mov	eax, dword [msg1+4]
    54 0000003B A3[12000000]            		mov	dword[msg2+4], eax
    55                                  	
    56 00000040 A1[08000000]            		mov	eax, dword [msg1+8]
    57 00000045 A3[16000000]            		mov	dword[msg2+8], eax
    58                                  	
    59 0000004A A1[0C000000]            		mov	eax, dword [msg1+12]
    60 0000004F A3[1A000000]            		mov	dword[msg2+12], eax
    61                                  
    62                                  
    63                                  
    64                                  ;;; print new msg2
    65 00000054 B804000000              		mov	eax, 4
    66 00000059 BB01000000              		mov	ebx, 1
    67 0000005E B9[0E000000]            		mov 	ecx, msg2
    68 00000063 BA0E000000              		mov	edx, msg1Len
    69 00000068 CD80                    		int	0x80
    70                                  
    71                                  ;;; exit
    72 0000006A BB00000000              		mov	ebx, 0
    73 0000006F B801000000              		mov	eax, 1
    74 00000074 CD80                    		int	0x80
    75                                  
    76