Difference between revisions of "CSC231 Bash Tutorial 5"

From dftwiki3
Jump to: navigation, search
(Hex, Binary, and 2's Complement)
(Hex, Binary, and 2's Complement)
Line 82: Line 82:
 
* Assemble, Link and run.  You should see eax, ebx, and ecx, as '''hex''', '''unsigned''', and '''signed''' (2's complement) integers.
 
* Assemble, Link and run.  You should see eax, ebx, and ecx, as '''hex''', '''unsigned''', and '''signed''' (2's complement) integers.
 
<br />
 
<br />
 +
=Bash Loops=
 +
<br />
 +
Bash supports loops!  It is a powerful way of repeating commands.
 +
<br />
 +
* Try this at the Bash prompt:
 +
 +
  for i in 1 2 3 4 5 6 7 8 9 10 ; do
 +
      echo "i = $i"
 +
  done
 +
 +
* Explanations:
 +
:::* '''i''' is used as the index of the loop.  It is declared by putting it right after the '''for''' keyword.
 +
:::* '''i''' will take all the values in whatever list is given after the '''in''' keyword.
 +
:::* the semi-colon ends the declaration, and is followed by '''do'''
 +
:::* the next lines will be repeated for every value '''i''' will take.  In our case, just the '''echo''' command, which is Bash's print command.
 +
:::* we close the body of the for-loop with '''done'''
 +
<br />
 +
<tanbox>
 +
Note that when you want to use the loop variable inside the body of the loop, you write '''$i'''.  Variables are declared without $ but are prefixed with $ when used.
 +
</tanbox>
 +
<bx>
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:Nasm]][[Category:CSC231]][[Category:Assembly]]

Revision as of 08:37, 3 March 2017

--D. Thiebaut (talk) 08:04, 3 March 2017 (EST)


Testing Instructions


Writing a short assembly program to answer quick questions is simple and can be quick. We'll do a couple examples.

Testing how DIV works


  • Get a copy of test_skel.asm
getcopy test_skel.asm

  • Its code is shown below for completeness:
;;; Testing sandbox
	        extern  _printDec
	        extern  _printString
	        extern  _println
	        extern  _getInput
		extern	_printRegs
	
	section	.data
a		dd	100
b		dd	33
c		dd	0
ans		dd	0
	
		section	.text
		global	_start
_start: ;-------------------------------------------------

	
;;; exit -------------------------------------------------
		mov	ebx, 0
		mov	eax, 1
		int     0x80


  • It has everything needed to test how short pieces of code work.
  • Put the following code between the dashed lines:


 		mov	edx, 0
		mov	eax, dword[a]
 		div	dword[b]
 
 		call	_printRegs
  • (If the lines are already there, but commented out, just remove the semi-colons.)
  • See what the program does. What quantity will be divided by what other quantity.
  • Assembly, link and run:
 nasm -f elf test_skel.asm
 ld -melf_i386 -o test_skel test_skel.o 231Lib.o
 ./test_skel 

  • Do you see the quotient and remainder in the registers? Does it make sense?


You should practice doing this simple exercise every time you are not sure about a particular set of instructions. If you start with a skeleton program, such as test_skel.asm, all you have to do is fill in the middle part, assemble, link and run, and you will have your answer.


Hex, Binary, and 2's Complement


  • You can use the same method for testing number representations.
  • Go to this URL http://www.nasm.us/doc/nasmdoc3.html, and locate Section 3.4.1. Look at the instructions and the different ways Nasm accepts literals.
  • Nasm accepts numbers in decimal, hex, binary, octal, and 2's complements.
  • Let's try a few:
  • Edit your test_skel.asm program and add the following lines between the dashed lines:


		mov	eax, 0x0000FFFF
		mov	ebx, 00000000111111110000000011111111b
		mov	ecx, 1111_1111_1111_1111_1111_1111_1111_1111b
		call	_printRegs


  • Assemble, Link and run. You should see eax, ebx, and ecx, as hex, unsigned, and signed (2's complement) integers.


Bash Loops


Bash supports loops! It is a powerful way of repeating commands.

  • Try this at the Bash prompt:
 for i in 1 2 3 4 5 6 7 8 9 10 ; do
      echo "i = $i"
 done

  • Explanations:
  • i is used as the index of the loop. It is declared by putting it right after the for keyword.
  • i will take all the values in whatever list is given after the in keyword.
  • the semi-colon ends the declaration, and is followed by do
  • the next lines will be repeated for every value i will take. In our case, just the echo command, which is Bash's print command.
  • we close the body of the for-loop with done


Note that when you want to use the loop variable inside the body of the loop, you write $i. Variables are declared without $ but are prefixed with $ when used.

<bx>