Sign in to follow this  

Assemly help

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi have a problem. I can't find any tutorials to learn x86 Assembly for windows. I found this code:
; First assembly program. This program asks for two integers as
3 ; input and prints out their sum.
4 ;
5 ; To create executable using djgpp:
6 ; nasm -f coff first.asm
7 ; gcc -o first first.o driver.c asm_io.o
8
9 %include "asm_io.inc"
10 ;
11 ; initialized data is put in the .data segment
12 ;
13 segment .data
14 ;
15 ; These labels refer to strings used for output
16 ;
17 prompt1 db "Enter a number: ", 0 ; don’t forget null terminator
18 prompt2 db "Enter another number: ", 0
19 outmsg1 db "You entered ", 0
20 outmsg2 db " and ", 0
21 outmsg3 db ", the sum of these is ", 0
22
23 ;
24 ; uninitialized data is put in the .bss segment
25 ;
26 segment .bss
27 ;
28 ; These labels refer to double words used to store the inputs
29 ;
30 input1 resd 1
31 input2 resd 1
32
33 ;
34 ; code is put in the .text segment
20 CHAPTER 1. INTRODUCTION
35 ;
36 segment .text
37 global _asm_main
38 _asm_main:
39 enter 0,0 ; setup routine
40 pusha
41
42 mov eax, prompt1 ; print out prompt
43 call print_string
44
45 call read_int ; read integer
46 mov [input1], eax ; store into input1
47
48 mov eax, prompt2 ; print out prompt
49 call print_string
50
51 call read_int ; read integer
52 mov [input2], eax ; store into input2
53
54 mov eax, [input1] ; eax = dword at input1
55 add eax, [input2] ; eax += dword at input2
56 mov ebx, eax ; ebx = eax
57
58 dump_regs 1 ; print out register values
59 dump_mem 2, outmsg1, 1 ; print out memory
60 ;
61 ; next print out result message as series of steps
62 ;
63 mov eax, outmsg1
64 call print_string ; print out first message
65 mov eax, [input1]
66 call print_int ; print out input1
67 mov eax, outmsg2
68 call print_string ; print out second message
69 mov eax, [input2]
70 call print_int ; print out input2
71 mov eax, outmsg3
72 call print_string ; print out third message
73 mov eax, ebx
74 call print_int ; print out sum (ebx)
75 call print_nl ; print new-line
76
1.4. CREATING A PROGRAM 21
77 popa
78 mov eax, 0 ; return back to C
79 leave
80 ret


But it is so different form this one: And why doesn't it start wiht ".386"??
.386 
.model flat,stdcall 
option casemap:none 
include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib 
include \masm32\include\user32.inc 
includelib \masm32\lib\user32.lib 

.data 
MsgBoxCaption  db "Iczelion Tutorial No.2",0 
MsgBoxText       db "Win32 Assembly is Great!",0 

.code 
start: 
invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK 
invoke ExitProcess, NULL 
end start 

The problem is that i can't fimd one version of Assembly and start learning! I want to learn how to loop in Assembly, how to select, how to make arrays, functions etc. and then go to deeper waters, if there are any. Isn't there any tutorial for this kind of Assembly? Which one of the 2 codes is better, and why are they different? and by the way i prefer console..

Share this post


Link to post
Share on other sites
The second one looks like no assembler I've ever seen - I know zilch about x86, but do know some z80. The second one appears to use macros; in the way that you do this in Z80:


ld a,3
ld (curcol),a
ld a,5
ld (currow),a
ld hl,text_string
bcall(_puts)
text_string:
.db "HELLO!",0


to display some text, but could "macrofy" it to do this:


; Stick the following in an external file to #include:
#define print_string(x,y,text) ld a,x \ ld (curcol),a \ ld a,y \ ld (currow),a \ ld hl,text \ bcall(_puts)

; This line would be in your "actual" source file.
print_string(3,5,text_string)

text_string:
.db "HELLO!",0


I hope that makes sense - the second one is like your second source file in that the actual assembler code has been turned into a macro, which can be less efficient.
It's a lot easier, of course... but you will gain more from using the first method.

Share this post


Link to post
Share on other sites
Quote:
Original post by sdlprorammer
why doesn't it start wiht ".386"??
Different syntax. The first one is for the NASM assembler, the second one is for MASM.

Quote:
Original post by sdlprorammer
Isn't there any tutorial for this kind of Assembly?
[google] ...ok, I'll throw in a search term as well: "Art of Assembly"

Quote:
Original post by sdlprorammer
Which one of the 2 codes is better, and why are they different?
They do completely different things... the first looks like it adds and prints out two numbers and dumps the registers and memory. The second one displays a message box titled "Iczelion Tutorial No.2" with the text "Win32 Assembly is Great!" and an "OK" button.

Share this post


Link to post
Share on other sites
i already found some tutorials but they are all different.

>Different syntax. The first one is for the NASM assembler, the second one is for MASM.

I want to learn Assembly. I want to learn standard assembly, without any implementation dependencies like those 2 codes, that will compile in all the compilers for windows. What will i do?

Share this post


Link to post
Share on other sites
There is no single standard for assembly syntax on x86, most of the variations are fairly similar in most respects though. If you want portable use a high level language - one of the design goals of C was for it to be a 'portable assembler'.

Share this post


Link to post
Share on other sites
There are a whole bunch of variations of assembler, here's a few compilers:
MASM32
NASM
A86

The examples you gave were from NASM and MASM32. The NASM example will output in DOS. MASM32 will give you a messagebox in Win32. MASM is macro based, but if don't want to use the macros you can still us the lower level version.

Here's some tutorials that can get ya started (Sorry for not linking them, in a hurry):
http://k2pts.home.comcast.net/gbaguy/x86asm.htm
http://www.drpaulcarter.com/pcasm/index.php
http://win32asm.cjb.net/
http://webster.cs.ucr.edu/ (More a bridge between Assembly and other High level languages)

Share this post


Link to post
Share on other sites
:'( NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO! no no no.

Can i use MASM32 for console programming? I don't want to make windows, just console prgramming. Also, low level activities such as opening ports, prinitng etc. are supported from all those compilers?

And finally, if you want to learn Assembly what do you do? Do you have to learn all those variations? Also, the book Art of Assembly, what assembler and variation does it use? Pls give some general things that you think i might want to know, although i read some stuff, it seems that what i read was not what i wanted to learn.. You are the experiecned..

Share this post


Link to post
Share on other sites
Yes you can use MASM for console programming in windows.

The Iczelion tutorials are a good start. The Windows API is not just for windowed programs. They can be used in console mode programs also.

Look for linkers settings /SUBSYSTEM:CONSOLE to build a console application. Use /SUBSYSTEM:WINDOWS to build a windows application.

Share this post


Link to post
Share on other sites
Thanks but the The Iczelion tutorials are not a good start. They teach how to build windows style programs, and they don't teach you Assembly - loops, ifs, structures, functions etc. Well they might teach you but i don't want to learn a bunch of useless stuff ( windows style programs ) before i learn how to use Assembly for Console. Where can i find this thing i am looking for? Assembly Language for console.

Also, low level activities such as opening ports, prinitng etc. are supported from all those compilers?

Also, the book Art of Assembly, what assembler and variation does it use?
thanks

Share this post


Link to post
Share on other sites
Quote:

...they don't teach you Assembly - loops, ifs, structures, functions etc...

Wow, do you even know what assembly language is all about?

Quote:

:'( why don't i get a reply... are there any assembly programmers here

And you're a whiner. Please go read "The Art of Assembly Language" and come back when you're done.

Share this post


Link to post
Share on other sites
Quote:
Original post by sdlprorammer
:'( why don't i get a reply... are there any assembly programmers here


You seem to be forgetting you're learning LOW level programming language. After experience with HIGH level ones.

It's totally different language. The assembler (the program) is just "convertor" of strings into machine code. You type nop and it's translated as 0x90. No compiling like you're used to.

There's no standard for the language. You've seen the differences between MASM and NASM - be glad you didn't find an example for GAS!

You can do everything with assembly. If the assembler doesn't support some instruction, just type it in machine code yourself.

But don't give up! I've learnt assembly on about 5th try [smile]. So if you fail on understanding something (or everything), just try it few weeks later [smile]

Oxyd

Share this post


Link to post
Share on other sites
i am trying but i need some help.

>Wow, do you even know what assembly language is all about?
i am trying to learn so

>And you're a whiner. Please go read "The Art of Assembly Language" [webster.cs.ucr.edu] and come back when you're done.

what's a whiner o_O ?

>If the assembler doesn't support some instruction, just type it in machine code yourself.

i don't get it. Am i not going to type anything in machine code?

and please:
Also, low level activities such as opening ports, prinitng etc. are supported from all those assemblers?

Also, the book Art of Assembly, what assembler and variation does it use?

thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by sdlprorammer
Also, low level activities such as opening ports
Believe it or not, that's not low level. There aren't any *hardware* ports (well, there are ports, but those are something else, poking bits to the hardware), those come from the network protocol you're using, which is actually part of Windows; no low-level hardware interface.

I can't remember which assembler Art of Assembly uses, but the information is relevant to pretty much all assemblers. Please read it before you go assuming things about the language. It will tell you _everything_.

Share this post


Link to post
Share on other sites
Look - the CPU executes machine code only. Machine code are just numbers in memory. The assembly is only a thing which gives theese numbers names. I already gave you an example - the procesor is able to execute 0x90 instruction. However - we don't want to memorize bunch of numbers (the instruction set is HUGE). So we gave it a name - "nop".

The assembler acts like a table - "nop" -> 0x90. BUT - some situation, like when the assembler doesn't have a name for an instruction (IIRC, GAS is not able to do 48bit long-jump (real mode to protected switch - clearing pipe), so the programmers have to type it in machine code.)

Quote:
Also, low level activities such as opening ports, prinitng etc. are supported from all those assemblers?


Yes... But the OS won't let you [smile]

I mean - sure - you may try to directly access LPT port. But the modern 32-bit OS's will crash your program when it tries it. You have to do the access using higher level API - I mean Win32API.

That's why it's called PROTECTED MODE operating system - it protects - from viruses etc. You have to ask the OS to every little bit of the computer (disk, heap memory, printer, screen, ...)

EDIT: And as Ra told: before you make any assumption from this post - read the Art of Assembly... And buy some book about assembly, if you're that kind that is not able to learn the basics from an e-book (like me).

Oxyd

Share this post


Link to post
Share on other sites
Quote:
Original post by sdlprorammer
Also, the book Art of Assembly, what assembler and variation does it use?


Off the top of my head it's written with an eye towards "HLA", which is the author's version of a high level macro language roughly similar to the macro sets used by NASM and MASM. The book isn't dependent on HLA and is highly recommended.

Assuming your targetting X86 cpu, you'll also want to get a copy of the manuals from Intel.

Search for "IA-32 Intel Architecture Software Developer’s Manual" and from there seek the specific cpu you're interested in.

There should be three volumes. The 2nd Volume, "Instruction Set Reference", is a must.

Share this post


Link to post
Share on other sites

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this