FILE_NAME". B..." /> FILE_NAME". B..." /> FILE_NAME". B..."/>

Swap vga text screen halves using x86 assembly

171 views Asked by At

We are supposed to swap the upper half with lower half, and my code does this but the problem is the last line is supposed to be the "C:>FILE_NAME". But the C in that line is on the second to the last. Also it is missing one line. I've been trying to play around with the numbers in CX but I'm not getting anywhere. Here is the code:

;Swap two halves of the screen crosswise

JMP START   ;This will start the program

START:
    MOV AX, 0B800H
    MOV ES, AX       ;Assign Video Display Area to ES
    MOV DS, AX      ;Assign Video Display Area to DS
    MOV SI, 0
    MOV DI, (80*2)*(12)  ;Lower half of screen

MOV CX, 13      ;Loop for half of screen

OUTERLOOP:
    PUSH CX
    MOV CX, 80  ;COLUMNS. I GET IT NOW!

INNERLOOP:
    MOV AL, [DS:SI]
    MOV B[ES:DI], AL
    ADD DI, 2

   MOV BL, [ES:DI]
   MOV B[ES:SI], BL
   ADD SI, 2 

LOOP INNERLOOP

   POP CX        

LOOP OUTERLOOP

INT 20H
1

There are 1 answers

1
Jester On

Your inner loop does the swapping wrong. The first block copies a character from column c in the top half to the bottom half, then the second block copies the character from column c+1 in the bottom to column c in the top. To swap, you should read the same column from both halves and write them back swapped. Such as:

MOV AL, [SI]
MOV BL, [DI]
MOV [SI], BL
MOV [DI], AL
ADD DI, 2
ADD SI, 2 

Also it's only copying a single byte, that is it doesn't copy the attribute (color). I don't know if that's intentional or not. You can change to 16 bit registers AX and BX and then it will copy attributes as well.

All the segment overrides are unnecessary and you only need to copy 12 lines, so set CX to 12.