News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Need help with an exercise

Started by verjolas, September 24, 2008, 06:08:23 PM

Previous topic - Next topic

verjolas

Hello, I'm very new to assembler and I need help.
I was given a program to do which needs to make an array of 21 cells where the first cell represents the size of the array. Now what I need to do is to move each cell to it's left side for example cell three to cell two cell four to cell three etc. I wrote some code but it doesn't work properly, can somebody tell me why? this is the code I wrote:
SSEG SEGMENT STACK
        STACK1 DB 100H DUP(?)
SSEG ENDS
DATA SEGMENT
        ARRAY DB 20H,1H,2H,3H,4H,5H,6H,7H,8H,9H,10H,11H,12H,13H,14H,15H,16H,17H,18H,19H,20H
DATA ENDS
CODE SEGMENT
        ASSUME CS:CODE,DS:DATA,SS:SSEG
        SHLOMI:
                MOV AX,DATA
                MOV DS,AX
                MOV SI,0
                MOV CL,[SI]
                MOV CH,0
                INC SI
                DORON:
                        MOV AL,[SI]
                        MOV AH,[SI+1]
                        MOV [SI+1],AL
                        MOV [SI],AH
                        INC SI
                LOOP DORON
                INT 3
        CODE ENDS
        END SHLOMI 
                       
thanks!

MichaelW

Hi verjolas,

I moved your post to the board where it belongs. Your code appears to do what you describe. Here is the initial state of your data:

0B79:0000  20 01 02 03 04 05 06 07-08 09 10 11 12 13 14 15
0B79:0010  16 17 18 19 20 00 00 00-

And here is the state after the loop has run to completion:

0B79:0000  20 02 03 04 05 06 07 08-09 10 11 12 13 14 15 16
0B79:0010  17 18 19 20 00 00 00 00-

Each loop is exchanging the values at [SI] and [SI+1]. While this does work, by your description all that is necessary is for each loop to move the value at [SI+1] to [SI].

eschew obfuscation

verjolas

but where does the 01 go? it should be at the end of the array if I'm not mistaken

MichaelW

The 01 is lost when your code loops past the end of your data. If you set the loop counter to 19d, then the 01 will be at the end of your data when the loop exits.
eschew obfuscation

verjolas

if I want the 01 to be at the end of the array what do I need to change here?

MichaelW

You need to load 19d (where the d means "decimal") into your loop counter (CX), instead of 20h (=32d).
eschew obfuscation

verjolas