Difference between revisions of "CSC231 Addressing Mode Exercises"

From dftwiki3
Jump to: navigation, search
(Exercise 4)
(Exercise 5)
Line 78: Line 78:
 
==Exercise 5==
 
==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.
 
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.
 +
 +
<font color="white">
 +
<code><pre>
 +
Powers  dd      57,0,0,0,0,0,0,0,0,0
 +
NOPOW  equ    ($-Powers)/4
 +
 +
        mov    ebx, Powers    ; ebx points to powers                         
 +
        mov    eax, dword[ebx]; get first dword                                         
 +
        mov    ecx, NOPOW-1    ; ready to loop 9 times                       
 +
 +
for:    shr    eax, 1          ; multiply eax by 2, store in next             
 +
        add    ebx, 4          ; point to next empty cell                     
 +
        mov    dword[ebx], eax ; cell of array                               
 +
        loop    for            ; go 9 times         
 +
</pre></code>
 +
</font>
  
 
==Exercise 6==
 
==Exercise 6==
 
Copy a string into another string, reversing the order of the string to see if they are palindromes.
 
Copy a string into another string, reversing the order of the string to see if they are palindromes.

Revision as of 11:38, 1 October 2008

Exercises on Addressing Modes

Exercise 1

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

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 2

Write a program that fills an array of 8 bytes with the first 8 fibonacci terms

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  

Exercise 3

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

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  

Exercise 4

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

 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          

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.

Powers  dd      57,0,0,0,0,0,0,0,0,0
NOPOW   equ     ($-Powers)/4

        mov     ebx, Powers     ; ebx points to powers                          
        mov     eax, dword[ebx]; get first dword                                          
        mov     ecx, NOPOW-1    ; ready to loop 9 times                         

for:    shr     eax, 1          ; multiply eax by 2, store in next              
        add     ebx, 4          ; point to next empty cell                      
        mov     dword[ebx], eax ; cell of array                                 
        loop    for             ; go 9 times          

Exercise 6

Copy a string into another string, reversing the order of the string to see if they are palindromes.