MASM - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

MASM

Description:

That means that from one system to the next there is a ... To use these arrays you must load the offset of one of them into a register. Here is an example. ... – PowerPoint PPT presentation

Number of Views:342
Avg rating:3.0/5.0
Slides: 31
Provided by: F223
Category:
Tags: masm | register

less

Transcript and Presenter's Notes

Title: MASM


1
MASM
  • ICS33

2
MASM
  • Assembly language is architecture dependent. That
    means that from one system to the next there is a
    different assembly language. We will be doing
    intel based assembly language. That includes
    athlons and cyrix chips if you were wondering.

3
VARIABLES
  • Variables are a little different than in other
    languages. In assembly language the computer
    doesn't care if it is a number, character, or
    string. It only cares about the size of the
    variable. The sizes include db, dw, and dd which
    stand for define byte, word, and double. A byte
    is 8 bits and can hold a number up to 256, a word
    is 16 bits and can hold a number up to about
    32000, a double is 32 bits and can hold a number
    up to about 65000. To create a variable the
    format is name size value. Here are some
    examples.

4
Variables
  • number dw 267
  • aLetter db 'F'
  • aString db "Hello World"
  • noValue db ?
  • The last one is uninitialized.

5
Basic Commands
  • mov
  • The command you will probably use the most is
    mov. It basically moves the source into the
    destination. The syntax is as follows mov
    dest,source. There are a few rules you must
    follow. For instance the destination can not be
    an immediate value. Also you can not move a
    variable into another variable. One thing to note
    is that the source is not affected by the mov
    operation. It is more of a copy than a move.

6
Basic Commands
  • Arithmetic Commands
  • The add command will add the source to the
    destination. The syntax is add dest,source. The
    sub command has the same syntax but it subtracts
    the source from the destination. Both of these
    commands follow the same rules about having the
    destination and source being variables. The inc
    and dec will increment and decrement respectively
    the operand given. The mul command takes only one
    operand and it multiplies it by whatever is in
    eax,ax,or al depending on the size of the
    operand.

7
Basic Command
  • The operand can not be an immediate value. The
    answer is put in ax. The div operation also takes
    just one operand which it divides into ax and
    puts the answer into al and the remainder into
    ah.

8
Basic Commands
  • Shift
  • The shl and shr operations will move the bits of
    the first operand to the number of times in the
    second operand. One cool use of this is for
    multiplication and division. The shl and shr are
    much faster than the mul and div operations. The
    shl function will multiply the first operand by
    2second operand. The shr is the same but it
    divides.

9
Loops and Conditions
  • Loops
  • Loops are pretty easy in assembly language. The
    first thing you want to do is to move into cx the
    number of times you want to loop. Then you create
    a label which can be called anything followed by
    a colon. Then you put all your code. When you
    want to loop, put loop then the name of the label
    you made without the colon. Here is an example.

10
Loops
  • mov cx,9
  • nameLabel2
  • inc si
  • inc di
  • loop nameLabel2
  • This code will increment si and di nine times.

11
Conditional Loops
  • Before you do any conditions you need to do a cmp
    operation. It compares the first to the second
    operand then sets some flags that you can use to
    determine the relation between the two. Then you
    can use operations like je, jne, jle, jg. They
    are short for jump equal, not equal, less than or
    equal, and greater than. If the condition is true
    it will jump to the operand you supplied. Here is
    an example.

12
Conditional Loops
  • mov cx,9
  • nameLabel2
  • cmp si,di
  • je anotherLabel
  • inc si
  • dec di
  • loop nameLabel2
  • anotherLabel

13
Conditional Loops
  • Here is an example of an if then else statement.
  • cmp bx,9
  • je label1
  • mov ax,5
  • jmp endLabel
  • label1
  • mov ax,4
  • endLabel

14
  • One thing that is different is that is backwards.
    The else part is first then the then part comes
    next.

15
While Loop
  • A while loop is done like this.
  • label1
  • cmp bx,4
  • je label2
  • inc bx
  • loop label1
  • label2
  • This while loop will continue until bx is equal
    to 4.

16
Nested Loops
  • This would be a good time to learn how to use the
    stack. Here is an example of a nested loop.

17
Nested Loops
  • mov cx,5
  • nameLabel
  • push cx
  • mov cx,9
  • nameLabel2
  • mov ax,0
  • mov ax,si
  • mov di,ax
  • inc si
  • inc di
  • loop nameLabel2
  • add di,23
  • pop cx
  • loop nameLabel

18
Nested Loops
  • As you can see we have two loops. The problem is
    that both of these loops use cx as their counter.
    To remedy this when we enter the loop we push the
    value of cx onto the stack and then when the
    inner loop is done we pop the top of the stack
    into cx.

19
Arrays
  • Here are some examples of different kinds of
    arrays.
  • someStrings db "hello world","hello 2 ","hello 3
    ",0
  • The first variable is an array of strings. One
    thing you should do is make all the strings the
    same length. It makes it a lot easier to use
    them.

20
Arrays
  • someNumbers dw 10 dup(?)
  • The second variable uses the dup command to make
    ten empty locations which you can set later on.
    You can put a number in those parentheses and it
    will put that number in each location.

21
Arrays
  • moreNumbers dw 2,1,2,5,5,6,3,2
  • The third variable is just an array of numbers.
    Each number needs to separated by a comma.

22
Arrays
  • someChars db 'adsf',0
  • The last variable is an array of characters.

23
Arrays
  • To use these arrays you must load the offset of
    one of them into a register. Here is an example.
  • mov si,offset someStrings
  • Once the offset is in the register you can access
    the values like this.
  • mov ax,si
  • That will put whatever is in the first position
    of si which would be the letter h not the whole
    string because a string is an array itself. To
    get to the next character do this.
  • inc si

24
Procedures and Macros
  • Procedures and macros are very similar to
    functions in c. They can be used to make
    repetitive tasks easier.

25
Procedures
  • Procedures
  • Do you remember the main proc that is in every
    program, well that is a procedure. A procedure
    can be created by giving it a name then putting
    proc after it. Put your code there. Then end it
    with a ret, then the name you chose then endp.
    You should put all this outside of the main
    procedure.

26
Procedures
  • Procedures
  • To use this procedure you use the call command
    and then the name of the procedure. Here is an
    example
  • .code
  • main proc
  • mov ax,_at_data
  • mov ds,ax
  • call myProc
  • mov ax,4c00h
  • int 21h
  • main endp
  • myProc proc
  • inc bx
  • ret
  • myProc endp

27
Procedures
  • This is how it works. When the procedure is
    called it goes down to the location of the
    procedure. It does it stuff then ret is called
    and it returns to where it was in main.

28
MACRO
  • A macro is very similar syntactically but it's
    inner workings is different. First it can be
    passed a parameter. Second instead of the program
    going to where the macro is located it pastes a
    copy of it into the program wherever it is
    called. For this reason it is not a good idea to
    use a macro in a loop. Here is an example of a
    macro.

29
MACRO
  • .code
  • main proc
  • mov ax,_at_data
  • mov ds,ax
  • aMacro 100
  • mov ax,4c00h
  • int 21h
  • main endp
  • aMacro macro theParameter
  • mov ax,theParameter
  • endm

30
MACRO
  • This macro is passed the value 100 and the macro
    puts that into ax.
Write a Comment
User Comments (0)
About PowerShow.com