Difference between revisions of "CSC231 Addressing Mode Exercises"

From dftwiki3
Jump to: navigation, search
(Exercise 1)
(Exercise 7)
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Exercises on Addressing Modes=
+
=Exercises on Addressing Modes and Loops=
 +
<br />
 +
<!--(Highlight the boxes to see one possible solution...)-->
 +
==Exercise 1==
 +
<br />
 +
Indicate the addressing used by each of the instructions below.
 +
<br />
 +
<source lang="asm">
 +
 
 +
;;;  ------------------------------------------------------------
 +
;;;  Identify possible errors in the instructions below, and
 +
;;;  indicate the addressing mode for each one.
 +
;;;  ------------------------------------------------------------
  
==Exercise 1==
+
                section .data
Write a program that changes all the characters of an all-uppercase string to all-lowercaseWe assume the string does not contain blank spaces
+
a              db      3
 +
b              db      0x12345678
 +
c              dw      0
 +
x              dd      30       
 +
array          dd      1,2,3,4,5,6,7,8,9,10
 +
       
 +
                section .text
 +
                global _start
  
<font color="white">
+
_start:        mov    eax, a
<code><pre>
+
                mov    eax, dword[a] ; is it an error?
msg     db      "hellotherehowareyou"
+
                mov    ebx, array
MSGLEN  equ     $-msg
+
                mov    eax, dword[ebx]
 +
                mov    esi, 0
 +
                mov    dword[ebx+esi], 0
 +
                mov    dword[ebx+esi+4], eax
 +
                mov    edi, b
 +
                mov     byte[edi], 'Z'
 +
                add     al, 'z'-'Z'
 +
                mov    ecx, 10
 +
for:            inc    ecx
 +
                loop    for
 +
   
 +
;;;  exit()
  
        mov    ebx, msg        ; ebx points to 1st char of msg                  
+
                mov    eax,1
        mov    ecx, MSGLEN    ; # of chars in string                         
+
                 mov    ebx,0
for:    sub     byte[ebx],32   ; lower to upper case, in memory               
+
                int     0x80   ; final system call
        inc    ebx            ; ebx points to next char                     
 
        loop    for
 
  
</pre></code>
+
</source>
</font>
+
<br />
  
 
==Exercise 2==
 
==Exercise 2==
Write a program that fills an array of 8 bytes with the first 8 fibonacci terms
+
Write a program that changes all the characters of an all-uppercase string to all-lowercase.  We assume the string does not contain blank spaces.  You can find an ASCII table [http://www.asciitable.com/ here].
 +
 
 +
 
 +
<!--
 +
msg    db      "HELLOTHEREHOWAREYOU"
 +
MSGLEN  equ    $-msg
 +
 +
        mov    ebx, msg        ; ebx points to 1st char of msg               
 +
        mov    ecx, MSGLEN    ; # of chars in string                         
 +
for:    sub    byte[ebx],32    ; lower to upper case, in memory               
 +
        inc    ebx            ; ebx points to next char                     
 +
        loop    for
 +
-->
  
 
==Exercise 3==
 
==Exercise 3==
 +
Write a program that fills an array of 8 bytes with the first 8 powers of 2: 1, 2, 4, 8, 16, etc.
 +
 +
<!--
 +
<code><pre>
 +
fib    db      1, 1, 0, 0, 0, 0, 0, 0
 +
NOFIB  equ    $-fib
 +
 +
        mov    ebx, fib
 +
        mov    esi, 2          ; point to Fib[3]                             
 +
        mov    ecx, NOFIB-2    ; skip first 2 Fibs                           
 +
        mov    al, byte[ebx+esi-1] ;                                         
 +
for:    add    al, byte[ebx+esi-2]
 +
        mov    byte[ebx+esi], al ; fib_i = fib_i-1 + fib_i-2                 
 +
        inc    esi              ; point to next uncomputed fib               
 +
        loop    for              ; go back 
 +
</pre></code>
 +
-->
 +
 +
==Exercise 4==
 
Write a program that fills an array of 16 words with the first 16 fibonacci terms
 
Write a program that fills an array of 16 words with the first 16 fibonacci terms
  
==Exercise 4==
+
<!--
 +
<code><pre>
 +
fib    dw      1, 1, 0, 0, 0, 0, 0, 0
 +
NOFIB  equ    ($-fib)/2
 +
 
 +
        mov    ebx, fib
 +
        mov    esi, 2*2        ; point to Fib[3]                             
 +
        mov    ecx, NOFIB-2*2  ; skip first 2 Fibs                           
 +
        mov    al, byte[ebx+esi-1*2] ;                                         
 +
for:    add    al, byte[ebx+esi-2*2]
 +
        mov    byte[ebx+esi], al ; fib_i = fib_i-1 + fib_i-2                 
 +
        add    esi,2              ; point to next uncomputed fib 
 +
       
 +
        loop    for              ; go back 
 +
</pre></code>
 +
-->
 +
 
 +
==Exercise 5==
 +
 
 
Write a program that fills an array of 10 double-words with the first 10 powers of 2.
 
Write a program that fills an array of 10 double-words with the first 10 powers of 2.
  
==Exercise 5==
+
<!--
Assume an array of 11 words, and the first words contains a constant.  Write the program that stores 1/2 the value of the constant in the 2nd word, 1/4 the value in the 3rd word, 1/8 4th word, 1/16 5th word, etc.
+
<code><pre>
 +
Powers  dd      0,0,0,0,0,0,0,0,0,0
 +
NOPOW  equ    ($-Powers)/4
 +
 
 +
        mov    ebx, Powers    ; ebx points to powers                         
 +
        mov    eax, 1
 +
        mov    dword[ebx], eax ; 2^0                                         
 +
        mov    ecx, NOPOW-1    ; ready to loop 9 times                       
 +
 
 +
for:    shl    eax, 1         ; multiply eax by 2, store in next             
 +
        mov    dword[ebx], eax ; cell of array                               
 +
        add    ebx, 4          ; point to next empty cell                     
 +
        loop    for            ; go 9 times         
 +
</pre></code>
 +
-->
  
 
==Exercise 6==
 
==Exercise 6==
Copy a string into another string, reversing the order of the string to see if they are palindromes.
+
The example below copies a string into another string, reversing the order of the string (to see if the original string is a palindrome, for example). Rewrite it using a ''based indexed'' addressing mode.
 +
 
 +
<!--
 +
msg1    db      "Esope reste ici et se repose"
 +
msg2    db      "                            "
 +
MSGLEN  equ    $-msg2
 +
 
 +
        mov    esi, msg1
 +
        mov    edi, msg2
 +
        mov    ecx, MSGLEN
 +
 
 +
for    mov    al, byte[esi]
 +
        mov    byte[edi], al
 +
        inc    esi
 +
        inc    edi
 +
        loop    for
 +
         
 +
-->

Latest revision as of 06:22, 2 October 2014

Exercises on Addressing Modes and Loops


Exercise 1


Indicate the addressing used by each of the instructions below.

;;;  ------------------------------------------------------------
;;;  Identify possible errors in the instructions below, and
;;;  indicate the addressing mode for each one.
;;;  ------------------------------------------------------------

                section .data
a               db      3
b               db      0x12345678
c               dw      0
x               dd      30        
array           dd      1,2,3,4,5,6,7,8,9,10
        
                section .text
                global  _start

_start:         mov     eax, a
                mov     eax, dword[a] ; is it an error?
                mov     ebx, array
                mov     eax, dword[ebx]
                mov     esi, 0
                mov     dword[ebx+esi], 0
                mov     dword[ebx+esi+4], eax
                mov     edi, b
                mov     byte[edi], 'Z'
                add     al, 'z'-'Z'
                mov     ecx, 10
for:            inc     ecx
                loop    for
    
;;;  exit()

                mov     eax,1
                mov     ebx,0
                int     0x80    ; final system call


Exercise 2

Write a program that changes all the characters of an all-uppercase string to all-lowercase. We assume the string does not contain blank spaces. You can find an ASCII table here.


Exercise 3

Write a program that fills an array of 8 bytes with the first 8 powers of 2: 1, 2, 4, 8, 16, etc.


Exercise 4

Write a program that fills an array of 16 words with the first 16 fibonacci terms


Exercise 5

Write a program that fills an array of 10 double-words with the first 10 powers of 2.


Exercise 6

The example below copies a string into another string, reversing the order of the string (to see if the original string is a palindrome, for example). Rewrite it using a based indexed addressing mode.