Macro definition
.Ones Macro A
Example invocation
.Ones 0000000000000101B
C++ Algorithm
int Ones( int N) {
int Ax = 0;
while (N != 0) {
if ( N & 1 == 1)
Ax++;
N = N >> 1;
}
return Ax;
}
|
Answer .Ones Macro A local L ifB <&A> exitm endif Mov eAx, 0 Push eCx Mov eCx, &A while&L: Cmp eCx, 0 Jne do&L Jmp endwhile&L do&L: if&L: Shr eCx Jc then&L Jmp endif&L then&L: Inc eAx endif&L: Jmp while&L endwhile&L: Pop eCx endM |
1b. (5) Give an accurate expansion of your macro definition using:
.Ones 0000000000000101B
Answer Mov eAx, 0 Push eCx Mov eCx, 0000000000000101B while$0001: Cmp eCx, 0 Jne do$0001 Jmp endwhile$0001 do$0001: if$0001: Shr eCx Jc then$0001 Jmp endif$0001 then$0001: Inc eAx endif$0001: Jmp while&L endwhile$0001: Pop eCx
2. The PROCEDURE named Compress accepts a zero byte terminated character string as input. It returns as output the input string with all blanks removed and a zero terminating byte. An example of a zero byte terminated string definition and the hexadecimal values stored in memory is:
Src Db ' b bd f',0 20 62 20 62 64 20 66 00 b b d f Procedure parameters are defined as follows: Input - Offset of the first character of input string. Output - Offset of the first character of output stringOn return to the calling procedure, the output string is to have all blanks removed. The C++ function prototype definition would be:
void Compress( char Input[], char Output[]);2a. (2) Write the corresponding assembly instructions to call Compress using the C++ statement of:
Compress( Src, Dst );
Invoke Compress, Addr Src, Addr Dst
2b. (18) Write the Compress function.
| void Compress( char Input[], char Output[]) { while (*Input != '\0') { if (*Input != ' ') { *Output = *Input; Output++; } Input++; } *Output='\0'; } |
INCLUDE Irvine32.inc
.data
src byte "a b c d", 0
dst byte 80 dup(0)
.code
Compress proc near C, input : near ptr byte,
output : near ptr byte
Mov eSi, input
Mov eDi, output
.WHILE byte ptr [eSi] != 0
.IF byte ptr [eSi] != ' '
Mov Al, [eSi]
Mov [eDi], Al
Inc eDi
.ENDIF
Inc eSi
.ENDW
Mov byte ptr [eDi], 0
Ret
Compress Endp
main proc
invoke Compress, ADDR src, ADDR dst
exit
main endp
end main
|
INCLUDE Irvine32.inc
.data
src byte "a b c d", 0
dst byte 80 dup(0)
.code
Compress proc near C, input : near ptr byte,
output : near ptr byte
Mov eSi, input
Mov eDi, output
Cld
.WHILE byte ptr [eSi] != 0
.IF byte ptr [eSi] != ' '
Movsb
.ELSE
Inc eSi
.ENDIF
.ENDW
Movsb
Ret
Compress Endp
main proc
invoke Compress, ADDR src, ADDR dst
exit
main endp
end main
end
|
3a. (5) The interrupt vector shows the segment and offset of the Divide routine executed when a Divide Overflow or Type 0 interrupt occurs. Fill in the interrupt vector assuming that Print is to be executed on a Type 5 interrupt and Break is to be executed on a Type 3 interrupt.
Interrupt Physical
Vector
Address Segment:Offset
| Offset 1401 | 00000
2B41 : 412B Print Proc Far
| Segment A114 | 00002
:
|
| 00004
Iret
|
| 00006
Print Endp
|
| 00008
|
| 0000A
2BAD : 12B4 Break Proc Far
| Offset 12B4 | 0000C
:
| Segment 2BAD | 0000E
Iret
|
| 00010
Break Endp
|
| 00012
| Offset 412B | 00014
A114 : 1401 Divide Proc Far
| Segment 2B41 | 00016
:
| Offset 1234 | 00018
Iret
| Segment 5678 | 0001A
Divide Endp
3b. (5) Show the stack contents and indicated registers after the execution of the Int 6 instruction below.
101A
Segment : Offset
| Flags | 1018
412C : A2B4 Int 6
| 412C |
1016
412C : A2B6 Mov Ax, 178
| A2B6 |
1014 <- Sp
|
| 1012
Sp 1014
Cs 5678
|
| 1010
|
| 100E
IF 0
Ip 1234
3c. (2) What is the Physical Address for IP=123416, CS=456716
456716*1016 = 4567016
+ 123416
468A416
4a. (2) Assume that the procedure OverFlow is to be invoked by a Type 6 interrupt. Give instructions necessary to initialize the interrupt vector to invoke OverFlow on the execution of the instruction:
Int 6
Mov Ax, 0
Mov Ds, Ax
Mov Bx, 6*4
Cli
Mov word ptr [Bx],offset Overflow ; [0001416]=offset Overflow
Mov word ptr [Bx]+2, Seg Overflow ; [0001616]=seg Overflow
Sti
4b. (18) Write the complete INTERRUPT procedure named OverFlow that prints the message of 'Overflow error' on a type 6 interrupt. Remember that interrupt procedures must not alter registers. Use the DOS function 9 to output the character string. Its definition is:
Int 21H, Function 09H, Output character string _________________________________________________________________ Call with: AH = 09 DS:DX = segment:offset of string Returns: Nothing Notes: The string must be terminated with the character '$', which is not output. _________________________________________________________________ Example: Output the string 'Hello'. Mov Ah, 9 Mov Dx, Seg String Mov Ds, Dx Mov Dx, Offset String Int 21h : String db 'Hello$'
OverFlow proc
Push Ax
Push Dx
Push Ds
Mov Ah, 9
Mov Dx, Seg String
Mov Ds, Dx
Mov Dx, Offset String
Int 21hPop Ds
Pop Dx
Pop Ax
IRet
String byte "Overflow error$"
OverFlow endp
void ReadString(char s[]) {
char Al;
int Cx=80;
do {
cin >>
Al;
*s = Al;
s++;
Cx--;
while (Cx != 0 && Al != 13);
s--;
*s = '\0';
}
Keyboard BIOS interrupt description _________________________________________________________________________ |Type 16h AH | Function Procedure Outputs | |_________________|_______________________________________________________| | 0 | Read Character Ah-register contains scan code | | | for next keyboard character | | | Al-register contains ASCII code | | | for next keyboard character | | | Character is deleted from buffer | | | | | 1 | Read buffer status ZF = 0 implies input buffer is | | | not empty. Ah-register contains | | | scan code for next keyboard | | | character Al-register contains | | | ASCII code for next keyboard | | | character. Character is not | | | deleted from buffer. | | | | |_________________|_______________________________________________________|
DOS input description _________________________________________________________________________ |Type 21h AH | Function Procedure Outputs | |_________________|_______________________________________________________| | 0 | Read Character Al-register contains ASCII code | | | for next keyboard character. | |_________________|_______________________________________________________|
void ReadString(char s[]) {
char Al;
int Cx=80;
do {
cin >> Al;
*s = Al;
s++;
Cx--;
while (Cx != 0 && Al != 13);
s--;
*s = '\0';
}
ReadString proc near C, s : near ptr byte mov eDi, s cld mov Cx, 80 do&: mov Ah, 0 int 16h stosb while&: cmp Al, 13 loopNe do& endwhile&: mov [eDi-1], 0 ret ReadString endp
6. (5) In the following:
- What device of the Launchpad does P1DIR.6 refer? _____red LED______
- What device of the Launchpad does P1DIR.3 refer? _____push button____
- What device of the Launchpad does P1IN.3 refer? _____push button____
- What assembly instruction turns on the green LED? _____none______
- What does the following code perform? _Place address of main in interrrupt vector at location of RESET operation_
.sect ".reset"
.short main
.cdecls C,LIST, "msp430G2231.h" .text main mov.w #0280h,SP ; Set stackpointer (128B RAM device) mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer bis.b #01000000b,&P1DIR ; Set P1DIR.6 = 1 bic.b #00001000b,&P1DIR ; Set P1DIR.3 = 0 bic.b #00001000b,&P1SEL ; Select Port 1 P1.3 bic.b #01000000b,&P1OUT ; Set P1OUT.6 = 0 _while ; while(1) { _if mov.b &P1IN, R15 ; if( P1IN.3 == 0) and.b #00001000b, R15 ; P1OUT.6 = 1 jz _then ; else jmp _else ; P1OUT.6 = 0 _then bis.b #01000000b, &P1OUT ; } jmp _endif _else bic.b #01000000b, &P1OUT _endif jmp _while _endwhile; Interrupt Vectors .sect ".reset" ; MSP430 RESET Vector .short main .end
7. (5) In the following:
- What device of the Launchpad does &CCR0 refer? ____Timer A count register___
- What device of the Launchpad does &TACTL refer? ____Timer A count control register___
- What device of the Launchpad does &CCTL0 refer? ____Timer A interrupt control register___
- &CCR0 count to generate a 2 second interrupt ? ___24,000_____________
- What does the following code perform? _Powers off the CPU and enables processor interrupts_
bis.w #CPUOFF+GIE,SR
.cdecls C,LIST, "msp430g2231.h" .text ; Progam Start main mov.w #0280h,SP ; Initialize stackpointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop Watch Dog Timer bis.b #01000001b,&P1DIR ; P1.0 and P1.6 output bic.b #00000001b,&P1OUT ; Set green LED off bis.b #01000000b,&P1OUT ; Set red LED onmov.w #10h,&CCTL0 ; CCR0 interrupt enabled, bit 4 = 1 mov.w #12000,&CCR0 ; Count 12000 to CCR0 mov.w #TASSEL_1+MC_1,&TACTL ; ACLK source (~12KHz), UP mode (count up to CCR0 value) bis.w #CPUOFF+GIE,SR ; CPU off, interrupts enabled TA0 xor.b #01000001b,&P1OUT ; Toggle P1.0 and P1.6 reti ; Interrupt Vectors .sect ".reset" ; MSP430 RESET Vector .short main ; .sect ".int09" ; Timer_A0 Vector .short TA0 .end