PDA

View Full Version : Assembly - Low Level Programming



Isomerizer
02-22-2010, 05:44 PM
I was wondering if anyone on here has any experience on Assembly programming and feels kind enough to share any tips/tutorials/advice , as this programming language completely confuses me!
I have recently started learning it as I must have a basic understanding of it on my degree.

I'm using 8086 TASM to edit .asm files and execute/link them to .exe. So far I have managed to create one script outputting "Hello Ben".


DOSSEG
.MODEL SMALL
.STACK 100h
.DATA
HelloMessage DB 'Hello, Ben',13,10,'$'
.CODE
mov ax,@data
mov ds,ax ;set DS to point to the data segment
mov ah,9 ;DOS print string function
mov dx,OFFSET HelloMessage ;point to "Hello, ben"
int 21h ;display "Hello, ben"
mov ah,4ch ;DOS terminate program function
int 21h ;terminate the program
END

I've also managed to mov and add on to some registers and play about with them a bit. I've got as far as handling int's...


.MODEL small
.STACK 200h
.DATA
buffer DB 20 DUP (0)
.CODE

mov ax,@data
mov ds,ax

mov ax,0 ; clear ax reg
mov bx,0 ; clean bx reg
mov cx,0 ; clear cx reg

mov al,-25
mov bl,-25h
mov cl,-101101b

mov al,25 ; decimal value 25
mov bl,25h ; 25 hexadecimal
mov cl,101101b ;45 in binary

add al,bl ; adds the contents of the two b-bit registers al and bl


mov ah,4ch ;termine the program and...
int 21h ;return to DOS
END

And have almost mastered how to use the basics of the debugging tool.

Any help at all will be highly appreciated!

Zeggy
02-22-2010, 06:19 PM
I only have experience with MIPS assembly so can't really help you with specifics :P

Might be able to help with general questions though.

PS. assembly is a headache :P


Edit:
The debugger is extremely useful. I don't know what debuggers you have for tasm but I'm sure there will be good ones.
Learn to use the debugger and you'll be able to understand the code better, instruction by instruction.

What level of understanding are you at right now and what do you need to know for your degree?
Do you understand registers and interrupts? Anything cpu-level?

Isomerizer
02-22-2010, 06:32 PM
What level of understanding are you at right now and what do you need to know for your degree?

I'm at a next to nothing level of understanding, I'm still trying to remember the flags/registers. I don't need to know anything too in depth, just the basics, maybe create a few basic scripts exampling a loop, arithmetic...
My degree is not a programming based computing degree, so we have minimal programming help. (I've been taught how to define a variable in python for the last 4 months...) This is why I ask here, I don't know how common Assembly is, just its low level and can be very powerful and fast. As you can probably guess, It's a completely new thing for me!

Where did you first learn? Any good & simple tutorials I can follow?
And what would you recommend doing first?

My assembly teacher also gave me the same advice to play around with the debugger, so I've been going over that.

What scripts have you created with it?

a_bertrand
02-22-2010, 06:49 PM
- Assembler is not a script.
- Assembler is nearly what runs on your CPU or as near as possible.
- You don't execute .asm files, you need to "convert" assembler files to your machine code.
- So you don't actually even compile assembler files ;)

Registers are the only few variables the CPU has access to. Depending on the CPU you have a different number of registers. To calculate something or do anything actually, you must normally read from the memory and put the data in one of the register, and then use that register to do something.

Flags are like modifiers or return status and can modify how things works or tell you how things went.

You may check:
http://www.xs4all.nl/~smit/asm01001.htm

Zeggy
02-22-2010, 06:53 PM
I learnt in university as well :)

I don't know of any tutorials for x86 assembly, like I said, I learnt mips assembly which is for a different cpu architecture. Wikibooks looks pretty good though: http://en.wikibooks.org/wiki/X86_Assembly

A good understanding of the hardware/cpu would definitely help, but isn't necessary. It can help you understand the program environment and visualize things better though, as assembly is quite close to hardware level.

Using the debugger is useful because you can run your program one instruction at a time, and view what's going on in the registers, in the memory, etc. You don't even need to be debugging a program, run a working program through it and see what's happening with each instruction. If your program is bugging, you can go through the (very tedious) process of running your program line by line, and comparing the values in registers to see if they are correct at each step.

I think x86 assembly is slightly more complicated because some instructions are actually many instructions put together into a 'macro' instruction. Then the language is easier to use but not as close to hardware level anymore. Your debugger should be able to show you what your instructions are being interpreted as though.


One of the first things I did was create a calculator. You can practice handling input, making calculations and displaying output. I also improved the calculator to handle more complicated expressions (reverse polish notation) by using the stack.

Isomerizer
02-22-2010, 06:56 PM
Thanks a_bertrand, any links appreciated!

Yep, I have to get into the habit of converting, linking, and then executing...

But what exactly is assembly used to created? Any examples? All i've seen is it outputting hello world... ^^

Edit: Nice Zeggy, still have the calculator around?

Zeggy
02-22-2010, 07:09 PM
Here's the basic one of just +,-,*,/:
http://www.pastebin.com/m49e5444d

Quite different instruction set than yours:
The LI, LA and MOVE instructions are equivalent to MOV. There's three because each only handles a specific data source (immediate, register, memory, address, etc).
The syscall is equivalent to int.
The j,jr,jal are for jumping to different parts of the code.

seanybob
02-22-2010, 07:54 PM
I took a class for (and mastered) the LC3 programming language... it's a psuedo-assembly language, in a sense.

Been recently teaching myself real assembly programming and it's use in 'cracking'. (I'm not cracking people's programs or any of that junk myself - just learning how other people do it to protect my own programs. Successfully done it on some test programs)

I could post some basic tutorials on it, but probably won't - too many script kiddies around here, not enough people who want to learn just for the sake of learning. I'll give it some thought though.