Reversing contents of an array
Page 1 of 1

Author:  adseybear [ Wed Feb 17, 2010 11:21 am ]
Post subject:  Reversing contents of an array

Using the example code i found on here,

* Program    : Array programming
* Written by : Chuck Kelly
* Date       : April, 2009
* Description: Get 10 numbers from keyboard, store them in
*              an array then display them from the array.
    ORG $1000
SIZE    equ     4   ; data size (4 bytes for long word)

START:              ; first instruction of program

    * read 10 numbers from keyboard and store in array
    lea array1,A0           ; pointer to array
    for D2 = #0 to #9*SIZE by #SIZE do.s  ; loop 10 times
        lea     prompt,A1   ; pointer to string
        move.b  #13,D0      ; display prompt
        trap    #15
        move.b  #4,D0       ; read number from keyboard into D1.L
        trap    #15
        move.l  D1,(A0,D2)  ; store number to array at index D2
    lea     result,A1       ; pointer to string
    move.b  #13,D0          ; display result
    trap    #15
    * display numbers from array
    for D2 = #0 to #9*SIZE by #SIZE do.s  ; loop 10 times
        move.l  (A0,D2),D1  ; get number from array at index D0
        move.b  #3,D0       ; display number in D1.L
        trap    #15
        lea     crlf,A1     ; pointer to string
        move.b  #14,D0      ; display cr lf
        trap    #15
    MOVE.B  #9,D0
    TRAP    #15     ; halt simulator

* Variables and Strings
array1  ds.l    10  ; 10 long word array
prompt  dc.b    'Enter a number: ',0    ; null terminated string
result  dc.b    'Your numbers are: ',0  ; null terminated string
crlf    dc.b    $d,$a,0                 ; carriage return, line feed, null

    END START       ; last line of source

I am struggling to work out how I would get the program to print the contents of the array in reverse order, any help greatly appreciated.


Author:  profkelly [ Wed Feb 17, 2010 1:09 pm ]
Post subject: 

Restructure the second FOR loop to make it loop from the last character in the array to the first.

Your question is about programming logic, not about 68000 programming.

The following is an excerpt from a lecture I give my students on a daily basis:

Logic first.
Logic first.
Logic first.

And while I'm lecturing, this is the student response:

Then when they ask me for :help:

This is my response:

Author:  demoskp [ Fri Feb 17, 2012 2:46 pm ]
Post subject:  Help

What do you mean restructure the second FOR loop to make it loop from the last character in the array to the first?

I am new to this can you tell me what to change please?

I will be grateful.

Author:  profkelly [ Fri Feb 17, 2012 3:00 pm ]
Post subject: 

I don't want to sound condescending in my responses to the first question or your question so please take this answer as that of a teacher trying to help a student.

I see a lot of students in Assembly Language who have weak programming logic skills. Those students can sometimes get through a lower level programming class that utilizes a high level language by finding example programs in the book or online and make changes to the code over and over until they get something that produces the desired result. That same approach will usually fail miserably in assembly language. (Which is one of the reasons I think every computer science degree should require it.)

Work out the logic required to solve the problem. Use flow charts or pseudo code to describe the logic. Code the solution in a high level language if necessary. Do not attempt to code the program in Assembly Language until you have a thorough grasp on the logic.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group