Write a simple program of your own to use BL, BH, BX, respectively a) MOV ten ch
ID: 3800186 • Letter: W
Question
Write a simple program of your own to use BL, BH, BX, respectively
a) MOV ten char ‘A’, ‘B’, ... ‘J’, ‘a’, ‘b’, ...’j’ into 8-bit registers. Upper case letter goes BH, lower case goes BL
b) Swap the information stored at BH and BL
c) Show the BX, BH, and BL before and after swapping
d) Continue your ASM programming and explore more registers AX/BX/CX/DX, AH/BH/CH/DH and AL/BL/CL/DL. Show these registers before and after swapping
*The program must be written in C++ format, not Assembly Language.
*Using Microsoft Visual Studio
*A similar example was to:
Swap bytes in two general registers and exam the relationship among BL, BH, BX
#include "stdio.h"
#include
int main(void)
{char temp;
char r1, r2;
short r;
_asm
{
MOV BL, 'a'
MOV BH, 'A'
MOV r1, BL
MOV r2, BH
MOV r, BX
}
printf("BH = %c, BL = %c ", r2, r1);
printf("BH = %c, BL = %c, BX = %x ", r2, printf("BH = %x, BL = %x, BX = %x ", r2, r1, r);
_asm
{
MOV temp,
BH MOV r2,
BL MOV BL, temp
MOV r2, BH
MOV r, BX
}
printf("This is the output after swapping ");printf("BH = %c, BL = %c, BX = %x ", r2, r1, r);printf("BH = %x, BL = %x, BX = %x ", r2, r1, r);
system("pause");
getchar();
return 0;
}
Explanation / Answer
/* Following program has been successfully executed in Microsoft visual studio.
Tried using all combinations AX/BX/CX/DX. It seems only BX (BH and BL data exchange are working fine.)
Code is proper. Please check in your system. */
#include<stdio.h>
#include<iostream>
using namespace std;
main()
{
//Initialise values to A and a to make use of for loop to get characters from B to J
char small = 'a';
char caps = 'A';
//Variables needed for printing on console
char r1,r2;
short r;
printf(" Exploring BX/BH/BL");
//Use for loop to increment characters from A to J (small letters and caps)
for(int i = 0;i < 10; i++)
{
//Following commented line can be added for debugging purpose
// printf(" Loop %d small : %c caps: %c ",i,small,caps);
_asm
{
MOV BL, small
MOV BH, caps
MOV r1,BL
MOV r2,BH
MOV r,BX
}
/* typecast to char to get the character printed */
char *ch1 = (char*)&r;
printf(" Before swapping BL = %c, BH = %c BX = %c%c",r1,r2,*(ch1+1),*ch1);
/* Swapping logic. Using AL */
_asm
{
MOV AL,BL
MOV BL,BH
MOV BH,AL
MOV r1,BL
MOV r2,BH
MOV r,BX
}
char *ch2 = (char*)&r;
printf(" After swapping BL = %c, BH = %c BX = %c%c",r1,r2,*(ch2+1),*ch2);
small = small+1;
caps = caps + 1;
} //End of for loop
printf(" Exploring AX/AH/AL");
small ='a';
caps = 'A';
//Use for loop to increment characters from A to J (small letters and caps)
for(int i = 0;i < 10; i++)
{
//Following commented line can be added for debugging purpose
// printf(" Loop %d small : %c caps: %c ",i,small,caps);
_asm
{
MOV AL, small
MOV AH, caps
MOV r1,AL
MOV r2,AH
MOV r,AX
}
/* typecast to char to get the character printed */
char *ch1 = (char*)&r;
printf(" Before swapping AL = %c, AH = %c AX = %c%c",r1,r2,*(ch1+1),*ch1);
/* Swapping logic. Using AL */
_asm
{
MOV BL,AL
MOV AL,AH
MOV AH,BL
MOV r1,AL
MOV r2,AH
MOV r,AX
}
char *ch2 = (char*)&r;
printf(" After swapping AL = %c, AH = %c AX = %c%c",r1,r2,*(ch2+1),*ch2);
small = small+1;
caps = caps + 1;
} //End of for loop
printf(" Exploring CX/CH/CL");
small ='a';
caps = 'A';
//Use for loop to increment characters from A to J (small letters and caps)
for(int i = 0;i < 10; i++)
{
//Following commented line can be added for debugging purpose
// printf(" Loop %d small : %c caps: %c ",i,small,caps);
_asm
{
MOV CL, small
MOV CH, caps
MOV r1,CL
MOV r2,CH
MOV r,CX
}
/* typecast to char to get the character printed */
char *ch1 = (char*)&r;
printf(" Before swapping CL = %c, CH = %c CX = %c%c",r1,r2,*(ch1+1),*ch1);
/* Swapping logic. Using AL */
_asm
{
MOV BL,CL
MOV CL,CH
MOV CH,BL
MOV r1,CL
MOV r2,CH
MOV r,CX
}
char *ch2 = (char*)&r;
printf(" After swapping CL = %c, CH = %c CX = %c%c",r1,r2,*(ch2+1),*ch2);
small = small+1;
caps = caps + 1;
} //End of for loop
printf(" Exploring DX/DH/DL");
small ='a';
caps = 'A';
//Use for loop to increment characters from A to J (small letters and caps)
for(int i = 0;i < 10; i++)
{
//Following commented line can be added for debugging purpose
// printf(" Loop %d small : %c caps: %c ",i,small,caps);
_asm
{
MOV DL, small
MOV DH, caps
MOV r1,DL
MOV r2,DH
MOV r,DX
}
/* typecast to char to get the character printed */
char *ch1 = (char*)&r;
printf(" Before swapping DL = %c, DH = %c DX = %c%c",r1,r2,*(ch1+1),*ch1);
/* Swapping logic. Using AL */
_asm
{
MOV BL,DL
MOV DL,DH
MOV DH,BL
MOV r1,DL
MOV r2,DH
MOV r,DX
}
char *ch2 = (char*)&r;
printf(" After swapping DL = %c, DH = %c DX = %c%c",r1,r2,*(ch2+1),*ch2);
small = small+1;
caps = caps + 1;
} //End of for loop
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.