Difference between revisions of "CSC231 Exercises with conditional jumps"

From dftwiki3
Jump to: navigation, search
(Finding the Smallest of 3 Ints)
 
(2 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=
  
Line 32: Line 35:
 
<br />
 
<br />
 
:Use a boolean function that returns true or false depending on whether the character it receives is lowercase or not.
 
: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 />
 
<br />
 
==Long/short jumps==
 
==Long/short jumps==
Line 47: Line 56:
 
<br />       
 
<br />       
 
:when the instruction at Label ''there'' is 1000 bytes away from the jl conditional jump?
 
: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 70: Line 79:
 
i        dd          0
 
i        dd          0
 
j        dd          0
 
j        dd          0
</pre></code>
+
</source>
-->
+
<br />
 
<br />
 
<br />
 
<br />
 
<br />

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