Difference between revisions of "CSC231 Exercises with conditional jumps"
(→Scanning an array) |
|||
(8 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= | ||
+ | <br /> | ||
__TOC__ | __TOC__ | ||
− | ==Finding the | + | <br /> |
− | + | <br /> | |
− | Find the minimum of 3 | + | <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 | + | :Find the '''largest''' element of a 1-dimensional array of integers. |
− | + | <br /> | |
− | |||
==Characters and lower/upper case conversion== | ==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== | ==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. | + | :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== | ==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: | + | :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 | cmp eax,10 | ||
jl there | jl there | ||
Line 38: | Line 53: | ||
there: ... | there: ... | ||
− | + | </source> | |
− | when the instruction at Label ''there'' is 1000 bytes away from the jl conditional jump? | + | <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. | ||
− | < | + | ::<source lang="asm"> |
;;; ------------------------------------------------------------ | ;;; ------------------------------------------------------------ | ||
;;; data areas | ;;; data areas | ||
Line 63: | Line 79: | ||
i dd 0 | i dd 0 | ||
j dd 0 | j dd 0 | ||
− | </ | + | </source> |
− | + | <br /> | |
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC231]][[Category:nasm]][[Category:Exercises]] |
Latest revision as of 08:48, 9 November 2015
--D. Thiebaut (talk) 08:08, 9 November 2015 (EST)
Exercises on Conditional Jumps
Contents
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