pc-technology masm and inline code in c. pc-teknik © caak2 masm and inline code in c some things to...
TRANSCRIPT
![Page 1: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/1.jpg)
PC-technology
MASM and Inline Code in C
![Page 2: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/2.jpg)
PC-Teknik © CAAK 2
MASM and Inline Code in C
• Some things to think about– Which size has the register you use
• AX, AH, AL• MOV AX, 500 Max 16 bits• MOV AH, 5 Max 8 bits• MOV AL,2 Max 8 bits• Now AX holds 0502H (1282 decimal)
– OUT always uses the accumulator, AX• in older versions of processors, only lower
byte, AL
![Page 3: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/3.jpg)
PC-Teknik © CAAK 3
MASM and Inline Code in C
• Some things to think about, cont.– MUL always works on AX
• MUL BL BL*AL the result in AX• MUL BX BX*AX the result in DX:AX
– DX*65536+AX because DX holds the 16 higher bits and AX the lower 16 bits
– When working with .exe-files, the code and data have to be placed in separate segments
![Page 4: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/4.jpg)
PC-Teknik © CAAK 4
One solution to the Organ
• Make an array of the numbers to divide the clock frequency with:– TONE DW 0, 0E24h, 11D1h, 0, 0,
10D0h, 0EFBh,0 – The first one is used when character A
is pressed, the next, the 0E24H when B is pressed, and 11D1H gives the tone C when C is pressed.
![Page 5: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/5.jpg)
5
One solution to the Organ• How do you get the right address?
– Ex. You press the C and want the tone C to sound. C has the ASCII code 67 and A has the code 65• If you take ‘A’-65 you get 0• If you take ‘C’-65 you get 2• When you go into the array the first number in the array is
at address TONE + 0, the next at address TONE+2, the next at TONE+4 => if C is pressed you want to goto address 2*2 which is (‘C’-65)*2
MOV BH,0 ;empty high byte of BXSUB BX,'A' ;ASCII-code - 65ADD BX,BX ; (ASCII code-65) * 2
![Page 6: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/6.jpg)
PC-Teknik © CAAK 6
One solution to the Organ
• How do you get the right address?– To point at address TONE:
• MOV SI,OFFSET TONE
– To point at the first byte of number corresponding to ‘C’
– MOV AL,DS:[SI+BX]
![Page 7: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/7.jpg)
7
Inline Code in C
• Links to get help with Inline code:– http://www.microsoft.com/
• in Swedish: http://www.microsoft.com/sverige/
– use the search function on Inline Assembly
• Links to other assembly language topics• http://www.jorgon.freeserve.co.uk/• http://home6.swipnet.se/~w-61969/asm/ASM-
NY.htm (in Swedish)• http://www.jegerlehner.ch/intel/• http://brianvictor.8m.com/
![Page 8: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/8.jpg)
8
Inline Code in C
• Usage– need a piece of program that runs
faster than the C-code– and want to use C code to type things
onto the screen– want to use the data segment in the
same way as in C
• Examples at MSDN
![Page 9: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/9.jpg)
9
# include <iostream.h>void main(void){
unsigned short number1, number2, regAX, regDX;long int product;number1=500;number2=500;_asm{
mov ax,number1mov bx,number2mul bxmov regAX,axmov regDX,dx
}cout<<regAX<<endl;cout<<regDX<<endl;product = regAX+65536*(long)regDX;cout<<product;
}
![Page 10: PC-technology MASM and Inline Code in C. PC-Teknik © CAAK2 MASM and Inline Code in C Some things to think about –Which size has the register you use AX,](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649e925503460f94b9782f/html5/thumbnails/10.jpg)
10
/* POWER2.C */#include <stdio.h>int power2( int num, int power );void main( void ){ printf( "3 times 2 to the power of 5 is %d\n", \power2( 3, 5) );}int power2( int num, int power ){ __asm { mov eax, num ; Get first argument mov ecx, power ; Get second argument shl eax, cl ; EAX = EAX * ( 2 to the power of CL ) } /* Return with result in EAX */}