Difference between revisions of "CSC231 Exercises with conditional jumps"

From dftwiki3
Jump to: navigation, search
(New page: =Exercises on Conditional Jumps= ==Finding the minimum== Find the minimum of 3 signed int variables a, b, and c ==Loop until overflow== Print fibonacci numbers coded as unsigned words ...)
 
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 08:08, 9 November 2015 (EST)
 +
----
 +
 
=Exercises on Conditional Jumps=
 
=Exercises on Conditional Jumps=
  
==Finding the minimum==
+
<br />
 
 
Find the minimum of 3 signed int variables a, b, and c
 
  
 +
__TOC__
 +
<br />
 +
<br />
 +
<br />
 +
==Finding the Smallest of 3 Ints==
 +
<br />
 +
:Find the minimum of 3  int variables a, b, and c.
 +
<br />
 +
<!--
 
==Loop until overflow==
 
==Loop until overflow==
 +
<br />
  
Print fibonacci numbers coded as unsigned words until the result overflows. Don't print erroneous numbers!
+
:Print fibonacci numbers coded as unsigned words until the result overflows. Don't print erroneous numbers!
  
 +
<br />
 +
-->
  
 
==Scanning an array==
 
==Scanning an array==
 +
<br />
 +
:Find the '''largest''' element of a 1-dimensional array of integers.
 +
<br />
  
Find the '''largest''' element of a 1-dimensional array of '''signed double-words'''.  
+
==Characters and lower/upper case conversion==
 +
<br />
 +
:Write the code necessary to transform a character from lower- to upper-case, if the character is a letter.  The character should not be changed if it is not a letter.
 +
<br />
 +
==Characters and lower/upper case conversion. Version 2==
 +
<br />
 +
:Use a boolean function that returns true or false depending on whether the character it receives is lowercase or not.
 +
<br />
 +
:What compilers often do:  Use the '''Carry''' bit to return True or False.  '''STC''' is an instruction that sets the carry bit to 1.  '''CLC''' is an instruction that clears the carry bit.  '''JC''' is an instruction that Jumps if the carry bit is set.  '''JNC''' is an instruction that jumps if the carry bit is not set.
 +
<br />
 +
==Execution Time==
 +
<br />
 +
* Assume we write a loop in java that will scan the text of the book Ulysses, by James Joyce, and transform it from upper- to lower-case.  The book contains 1.5 million characters.  How long would it take a 1 GHz Pentium to scan the whole string, assumed to be already in memory?
 +
<br />
 +
==Long/short jumps==
 +
<br />
 +
:Conditional jumps can jump only +127 bytes down, -128 bytes up in the code. How can we code something like this:
 +
<br />
 +
::<source lang="asm">
 +
                  cmp    eax,10
 +
                  jl      there
 +
                  ...
 +
                  ...
 +
        there:  ...
  
Same question, but with  '''unsigned double words'''.
+
</source>
 +
<br />     
 +
:when the instruction at Label ''there'' is 1000 bytes away from the jl conditional jump?
 +
<br />
  
 
==Print 2-dimensional arrays==
 
==Print 2-dimensional arrays==
  
 
Write the code necessary for printing an array  of '''chars''' (maze) using ''for-loops'' depending on i and j indexes.
 
Write the code necessary for printing an array  of '''chars''' (maze) using ''for-loops'' depending on i and j indexes.
<code><pre>
+
::<source lang="asm">
 
;;;  ------------------------------------------------------------
 
;;;  ------------------------------------------------------------
 
;;;  data areas
 
;;;  data areas
Line 37: Line 79:
 
i        dd          0
 
i        dd          0
 
j        dd          0
 
j        dd          0
</pre></code>
+
</source>
 
+
<br />
==Characters and lower/upper case conversion==
+
<br />
 
+
<br />
Convert a character to ''upper-case'', no matter what ASCII code it contains (You may want to look at [[CSC231_makeUpper.asm | makeUpper.asm]].
+
<br />
 
+
<br />
==Long/short jumps==
+
<br />
 
+
<br />
Conditional jumps can jump only +127 bytes down, -128 bytes up in the code. How can we code something like this:
+
<br />
 
+
<br />
                  cmp    eax,10
+
<br />
                  jl      there
+
<br />
                  ...
+
<br />
                  ...
+
<br />
        there:   ...
+
<br />
 
+
<br />
     
+
[[Category:CSC231]][[Category:nasm]][[Category:Exercises]]
when the instruction at Label ''there'' is 1000 bytes away from the jl conditional jump?
 

Latest revision as of 08:48, 9 November 2015

--D. Thiebaut (talk) 08:08, 9 November 2015 (EST)


Exercises on Conditional Jumps





Finding the Smallest of 3 Ints


Find the minimum of 3 int variables a, b, and c.


Scanning an array


Find the largest element of a 1-dimensional array of integers.


Characters and lower/upper case conversion


Write the code necessary to transform a character from lower- to upper-case, if the character is a letter. The character should not be changed if it is not a letter.


Characters and lower/upper case conversion. Version 2


Use a boolean function that returns true or false depending on whether the character it receives is lowercase or not.


What compilers often do: Use the Carry bit to return True or False. STC is an instruction that sets the carry bit to 1. CLC is an instruction that clears the carry bit. JC is an instruction that Jumps if the carry bit is set. JNC is an instruction that jumps if the carry bit is not set.


Execution Time


  • Assume we write a loop in java that will scan the text of the book Ulysses, by James Joyce, and transform it from upper- to lower-case. The book contains 1.5 million characters. How long would it take a 1 GHz Pentium to scan the whole string, assumed to be already in memory?


Long/short jumps


Conditional jumps can jump only +127 bytes down, -128 bytes up in the code. How can we code something like this:


                  cmp     eax,10
                  jl      there
                  ...
                  ...
         there:   ...


when the instruction at Label there is 1000 bytes away from the jl conditional jump?


Print 2-dimensional arrays

Write the code necessary for printing an array of chars (maze) using for-loops depending on i and j indexes.

;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

        section .data
maze    db "################################"     
maze2   db "            #  #           #    "     
        db "#########   #  #  #######  #   #"
        db "#           #  #        #      #"
        db "#########         ##############"
        db "#           ####               #"
        db "################################"
C       equ maze2-maze  ; num of columns
R       equ 7           ; num of rows

i        dd          0
j        dd          0