-
February 12th, 2002, 08:42 AM
#1
Tutorial: The basics of 80x86 assembly with MASM 5.0 II
First, you can find MASM.EXE and LINK.EXE at
http://users.ece.gatech.edu/~hamblen/489X/iotest/
This site has MASM 5.0 on it, which is plenty enough for what we are doing. Masm32 is a GREAT assembler, but has trouble with DOS apps, and we REALLY don't want to go into the Windows API in assembly, but if you want to learn to do that you can goto: http://spiff.tripnet.se/~iczelion as he has some of the best win32 programming guides I have ever seen.
Now that we have the EASY stuff over this tutorial will go into a small amount of detail and have a nifty code example of how to do conditional jumps (sort of like an IF then statement).
When we last left our coding we had learned how to call one procedure from another, this is useful, but please, do NOT try to make a loop like this, it WILL go infinate and it will freeze your machine. (just thought I would warn you). Since I like to code SAFELY I will not cover the actual LOOP instruction (yet), instead I shall cover conditional jumping which will allow you to make "safe" loops with little danger of going unbounded and locking the machine. First of all, we must understand that MASM allows us to label locations in the code (with debug we would have to jump to addresses not to a label). To specify a label type a label name followed by a colon so "start:" would be a label we can jump to.
Now let's go over all the possible logic for jumping. Here they are:
*************************************************
jmp-->always jump
jne-->Jump if not equal to
jnz-->same as jne
ja-->jump if greater than
jae-->jump if greater than or equal to
jb-->jump if less than
jbe-->jump if less than or equal to
jna-->jumps if the first number was NOT above
jnae--> jumps if The first number was NOT above or equal to
jnb-->jumps if the first number is not below
jnbe-->jumps if the first number is not below or equal to
jz-->jumps if the numbers are equal
je-->same as jz
jc-->Jump if carry flag is set (will not cover this one)
The jump can only be up to 127 bytes in either direction.
*************************************************
Now to understand how to code a jump, first of all we need to know how we are comparing, for this we use the 'cmp' instruction. The CMP instruction compares two values of equal size (either 8 bit registers, 16 bit registers, or two variables) and goes to the next instruction in the code (this will be the jump instruction). Now some code to show how this works. I am assuming the user has already downloaded (and learned to read) the interrupt list I gave the address for in the last tutorial.
.model small
.stack
.data
Message db "Press Y or N:$" ;Prompt for user
Uyes db "You pressed Y!$" ;Pressed y
Uno db "You pressed N!$" ;Pressed n
;###########################################################;Start of Code
;###########################################################
.code
start:
mov ax,03h ;clears screen (function 3h)
int 10h ;interrupt 10h
mov ax, SEG Message ;put segment of message into AX
mov ds, ax ;put this into DS
mov dx, OFFSET Message ;put offset of message into AX
mov ah,09h ;Function 9h of
int 21h ;Interrupt 21h
mov ah,01h ;function 01h of int21h,
int 21h ;get char from keyboard
cmp al, "Y" ;if ah Y then
je Yes ;Goto Yes label
cmp al, "N" ;if ah N then
je No ;Goto No Label
jne start ;if not Y or N then goto start
Yes:
mov ax,03h ;clears screen (function 3h)
int 10h ;interrupt 10h
mov ax, SEG Uyes ;segment of Uyes
mov ds, ax ;put segment into DS
mov dx, OFFSET Uyes ;put offset of Uyes into DX
mov ah,09h ;function 9h print string at DS X
int 21h ;call interrupt 21h
jmp close
No:
mov ax,03h ;clears screen (function 3h)
int 10h ;interrupt 10h
mov ax, SEG Uno ;segment of Uno
mov ds, ax ;put segment into DS
mov dx, OFFSET Uno ;put offset of Uno into DX
mov ah,09h ;function 9h print string at DS X
int 21h ;call interrupt 21h
jmp close
;###########################################################
;Close routine
;###########################################################
close:
mov ax,4c00h ;put 4c00h into ax, closing back to DOS
int 21h ;INT 21h, return to DOS
end start
I believe with the comments this code is pretty self explainatory. It basically prompts the user for a choice Y or N. If the user chooses an invalid choice, the program loops back to start.
Well until next time I guess I shall say goodbye, time for some sleep, and to get off work!
Cheeseball
PS: this code looks crappy in a forum, if you want an easier to READ version goto: www25.brinkster.com/cheeseball/asmtut2.htm
-
February 13th, 2002, 06:13 AM
#2
hey that was another good post...great man i am learning ... from u cheeseball....
thanks ... pls keep posting..... i am waiting for part III...
thanks once again...
intruder...
A laptop, internet connection and beer.
-
February 18th, 2002, 01:50 AM
#3
Junior Member
Very nice, Although I wish to learn Assembly from a book, for they do give more information, then you could give on one post, But I found your tutorial a 5 star rating ( out of 5 ).. Good Job.
-
February 19th, 2002, 02:40 PM
#4
hey cheeseball i was having one doubt i studied all your programs which u have
written in your tutorial. in the program which prints "hello world" if we want to
print two strings ..( i tried that ) but they are comming side by side.
hey is there any escape character in assembly so that we can print the second
string in second line.???
and pls post something more about the assembly i am waiting for your tutorial part III
thanks once again..
intruder..
A laptop, internet connection and beer.
-
February 20th, 2002, 12:15 AM
#5
OOPS, forgot something VERY important, if you want to print a "newline" you have to do charecters 10 and character 13, this is for a newline and a carriage return (yes you have to do both). you can append this to your string by doing a
string db "my string is here", 10, 13,"$"
string2 db "string 2 is here", 13,10, "$"
it does not matter which one you do first, you can also specify them as constants, but that is a little bit of a longer post. Sorry I haven't posted anything new, I have started on a "project" to kill my boredom and maybe make my resume look better.
Cheeseball
-
February 20th, 2002, 12:20 AM
#6
nice tut cheese! next time use the [/code] tags instead of [/quote] - it's monospaced, and looks a little bit better...
Code:
And you can
do things
like
this!
!
!
!
I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.
-
February 20th, 2002, 05:41 AM
#7
hey thanks for that cheeseball.. i was waiting for that ..
thanks once again..
intruder
A laptop, internet connection and beer.
-
February 20th, 2002, 07:27 AM
#8
There are code tags?? Wow....that is nice... What I am currently working on is a troubleshooting tool to keep me from taking 90 year old ladies into DOS to do pings, pull network config, system information, check DNS, pull the current IP, and stuff like that.....I have 4 modules in it and working decently ok right now.
-
February 22nd, 2003, 08:47 AM
#9
Junior Member
hi cheese it seems that ur little step proves a great leap for me
this tut is easily digestable and usable thanks
grat job that needs an hats off
hello cheese do have any tutorial for a bios programming
cheese i have a prob pls try to solve it my frnd actually programmed the whole bios and its very good program can u help me by making me learn bios programming
-
March 3rd, 2003, 05:05 PM
#10
Chesseball good work! I notice U refered to masmv5 if U are using it I would sugguest using Masmv8 it's better. Looking at your code I challenge myself to optimize it off the top of my head.
Code:
.code
start:
mov ax, 03h
int 10h
mov dx, message
mov ah, 09h
int 2fh
mov ah, 01h
int 2fh
cmp al
je No
jne start
Yes:
mov ax 03h
int 10h
mov dx, Uyes
mov ah, 09h
int 2fh
jmp close
No:
mov ax,03h
int 10h
mov dx, Uno
mov ah,09h
int 2fh
close:
int 20h
remember it's off my head this would work also. U didn't need the "jmp close instruction" after the code in the 'No lablel' it's only wasted processor time and slows program down.
but I have to give to U Cheeseball keep it up and maybe U'll convert the C and C++ heathens%-<|> anyway good work
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|