 |

10-10-2010, 03:48 PM
|
 |
Hero
|
|
Join Date: Apr 2005
Posts: 7,068
|
|
x86 assembly with MASM
Oookay, so x86 assembly.
If you have any version of visual c++ > 6.0 installed, you have MASM and everything you need to use it.
If you have VC++6, you can download the Visual C++ Processor Pack, which includes MASM 6.15.
Create a new text file and save it as addtwo.asm
Code:
.386 ;architecture
.model small,c ;pretty sure this lets you use __cdecl conventions
.data ;section for variables
fmt db "Sum is %d",0 ;string declared
.code
INCLUDELIB MSVCRT ;so you can link the vc functions
EXTRN printf:NEAR ;namely, printf...
EXTRN exit:NEAR ;and exit
PUBLIC main ;main function.. just like c
main: ;label
mov eax,5 ;put 5 in the eax register
mov ebx,8 ;put 8 in the ebx register
add eax,ebx ; add the contents of eax and ebx and put the result in eax
push eax ; push eax onto the stack
mov ebx, offset fmt ; put fmt in ebx
push ebx ; push ebx onto the stack
call printf ;call printf(fmt,13)
nop ;delay slot
mov eax,0 ;put 0 in eax
push eax ; put eax on the stack
call exit ; call exit(0)
nop ; delay slot
END ; end directive
Compile by navigating command prompt to wherever you saved this and typing ML /coff addtwo.asm, then run addtwo. your output should be
"Sum is 13"
|

10-11-2010, 10:55 PM
|
|
Guru
|
|
Join Date: Mar 2009
Location: Efnet
Posts: 1,061
|
|
Re: x86 assembly with MASM
Quote:
Originally Posted by SMR
Oookay, so x86 assembly.
If you have any version of visual c++ > 6.0 installed, you have MASM and everything you need to use it.
If you have VC++6, you can download the Visual C++ Processor Pack, which includes MASM 6.15.
Create a new text file and save it as addtwo.asm
Code:
.386 ;architecture
.model small,c ;pretty sure this lets you use __cdecl conventions
.data ;section for variables
fmt db "Sum is %d",0 ;string declared
.code
INCLUDELIB MSVCRT ;so you can link the vc functions
EXTRN printf:NEAR ;namely, printf...
EXTRN exit:NEAR ;and exit
PUBLIC main ;main function.. just like c
main: ;label
mov eax,5 ;put 5 in the eax register
mov ebx,8 ;put 8 in the ebx register
add eax,ebx ; add the contents of eax and ebx and put the result in eax
push eax ; push eax onto the stack
mov ebx, offset fmt ; put fmt in ebx
push ebx ; push ebx onto the stack
call printf ;call printf(fmt,13)
nop ;delay slot
mov eax,0 ;put 0 in eax
push eax ; put eax on the stack
call exit ; call exit(0)
nop ; delay slot
END ; end directive
Compile by navigating command prompt to wherever you saved this and typing ML /coff addtwo.asm, then run addtwo. your output should be
"Sum is 13"
|
x86 Assembly != C/C++
|

10-11-2010, 11:15 PM
|
 |
Hero
|
|
Join Date: Apr 2005
Posts: 7,068
|
|
Re: x86 assembly with MASM
I know that...
|

10-27-2010, 10:15 PM
|
 |
Director
|
|
Join Date: Apr 2005
Posts: 4,013
|
|
Re: x86 assembly with MASM
Quote:
Originally Posted by SMR
Oookay, so x86 assembly.
If you have any version of visual c++ > 6.0 installed, you have MASM and everything you need to use it.
If you have VC++6, you can download the Visual C++ Processor Pack, which includes MASM 6.15.
Create a new text file and save it as addtwo.asm
Code:
.386 ;architecture
.model small,c ;pretty sure this lets you use __cdecl conventions
.data ;section for variables
fmt db "Sum is %d",0 ;string declared
.code
INCLUDELIB MSVCRT ;so you can link the vc functions
EXTRN printf:NEAR ;namely, printf...
EXTRN exit:NEAR ;and exit
PUBLIC main ;main function.. just like c
main: ;label
mov eax,5 ;put 5 in the eax register
mov ebx,8 ;put 8 in the ebx register
add eax,ebx ; add the contents of eax and ebx and put the result in eax
push eax ; push eax onto the stack
mov ebx, offset fmt ; put fmt in ebx
push ebx ; push ebx onto the stack
call printf ;call printf(fmt,13)
nop ;delay slot
mov eax,0 ;put 0 in eax
push eax ; put eax on the stack
call exit ; call exit(0)
nop ; delay slot
END ; end directive
Compile by navigating command prompt to wherever you saved this and typing ML /coff addtwo.asm, then run addtwo. your output should be
"Sum is 13"
|
SMR, perhaps your productive energies would be better spent learning how to write a compiler  I took an honours course in compilers last semester, and it was fantastic. I can probably help you out if you get stuck.
|

10-27-2010, 10:46 PM
|
 |
Hero
|
|
Join Date: Apr 2005
Posts: 7,068
|
|
Re: x86 assembly with MASM
Quote:
Originally Posted by Sythe
SMR, perhaps your productive energies would be better spent learning how to write a compiler  I took an honours course in compilers last semester, and it was fantastic. I can probably help you out if you get stuck.
|
Hey Sythe, I'm presently taking comp sci courses which are required for my degree and are prerequisites to compiler design. Browsing my uni's course catalog, 'CPSC 411 - Compiler Construction I' - is a fourth year course. When I get there, I'd definitely ask you for help if I need it.
You may recall telling me a bit about compiler design and sythescript a while ago, too.
Last edited by SMR : 10-28-2010 at 03:37 AM.
|

10-28-2010, 09:05 AM
|
|
Newcomer
|
|
Join Date: Dec 2009
Posts: 13
|
|
Re: x86 assembly with MASM
Here is the NASM version. it prompts the user to input two numbers and finds their sum.
assemble it with NASM
i.e lets call it sum.asm
Code:
nasm -f coff sum.asm
then create a video driver in C for input/output i.e sum.c
Code:
int main()
{
/*we call our calc() function*/
calc();
return 0;
}
To compile using DJGPP or GCC use:
Code:
gcc -o sum sum.o sum.c
if you are using GCC remove the underscores from all our functions in the asm code.ie just use printf, scanf, getch with no underscores
ok here we go
Code:
;
; this segment contains initialised data
segment .data
fmt db "%s",0
msg db "Please enter a number: ",0
msgo db "The sum is %d",0
par db "%d",0
;code goes here
segment .text
global _calc ;declare our function calc(),as global
extern _printf,_scanf,_getch ;declare prinf(),scanf()& getch() as external
_calc:
push ebp ;save the base pointer register on to the stack and
mov ebp,esp ;make it equal to stack pointer as per cdecl conventions
pusha ;save all general purpose registers on to the stack
push msg ;push function parameters on to the stack in reverse order
push fmt ; this is equal to ...
call _printf ; printf("%s",msg)
lea ecx,[ebp-4] ;store into ecx the memory address of the first local variable inside
;the calc() function ie.int calc(){ int x,y; } so on the stack x
; is at [ebp-4]
push ecx ;now ecx is a pointer to variable x at [ebp-4] so
push par ;we get a value from the keyboard and store it at x[ebp-4];
call _scanf ;ie scanf("%d",&x);
push msg
push fmt
call _printf
lea edx,[ebp-8] ;we load the address of the second variable i.e y into edx
;now edx is a pointer to y ie int calc(){int x; int y;}
push edx ; int x; int* edx; edx =&y;
push par ; so *edx = 2 means y = 2
call _scanf ;so again we scanf("%d",&y)
mov ebx,dword[ebp-4] ;move the value inside our variable x into ebx
mov eax,dword[ebp-8] ;move the value of y into eax
add eax,ebx ;addition, eax += ebx, eax = eax+ebx, y+=x;
push eax
push msgo
call _printf ;printf("The sum is %d",y);
call _getch ;call the getch() function
add esp,40 ;we pushed 10 double words i.e sizeof(int)x10 onto the stack
; so we clean it up by adding 40 onto esp.a shortcut
popa ;pop all general purpose registers from the stack
mov esp,ebp ;restore the original value of esp
pop ebp ;pop ebp out of the stack
ret ;return to the caller function.
its all equal to
Code:
int calc();
int main()
{
calc();
}
int calc()
{
int x, y;
printf("Please enter a number");
scanf("%d",&x);
printf("please enter a number");
scanf("%d",&y);
y+=x;
printf("%d",y);
return 0;
}
Last edited by Ashken : 10-28-2010 at 09:18 AM.
Reason: some additions
|

10-28-2010, 01:50 PM
|
 |
Hero
|
|
Join Date: Apr 2005
Posts: 7,068
|
|
Re: x86 assembly with MASM
Equivalent C code would actually be:
Code:
int __cdecl printf(const char * format, ...);
void __cdecl exit(int status);
#pragma comment(linker, "/NODEFAULTLIB")
#pragma comment(lib, "MSVCRT.LIB")
main()
{
const char *fmt = "Sum is %d";
int a, b;
a = 5;
b = 8;
a = a + b;
printf(fmt,a);
a = 0;
exit(a);
}
Executables are the same size but slightly different because the asm example uses registers whereas the C code uses nothing but the stack. But with optimizations (specifically /opt:ref) the linker will ignore any .lib/.dll references that aren't actually used, so the pragmas may not be necessary.
Last edited by SMR : 10-28-2010 at 03:20 PM.
|
 |
|