ASSEMBLY LANGUAGE FUNDAMENTALS - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

ASSEMBLY LANGUAGE FUNDAMENTALS

Description:

Characters and integers are one and the same, as far as the assembler is concerned. ... This would be a good time to learn how to use the stack. Here is an ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 31
Provided by: F223
Category:

less

Transcript and Presenter's Notes

Title: ASSEMBLY LANGUAGE FUNDAMENTALS


1
ASSEMBLY LANGUAGE FUNDAMENTALS
  • ICS33

2
DATA DEFINITION DIRECTIVES
  • A variable is a symbolic name for a location in
    memory where some data are stored.
  • In assembly language, variables are identified by
    labels. Each label shows the starting location of
    a variable. We say that the labels offset is the
    distance from the beginning of the segment to the
    beginning of the variable.

3
DATA DEFINITION DIRECTIVES
  • A label however, does not indicate how many bytes
    of storage are allocated for a variable. For
    example, if we declare an array of 14 characters,
    the label aList identifies only the offset of the
    first character (A)
  • Declaration Stored As

aList db ABCD
4
DATA DEFINITION DIRECTIVES
  • So if the first letter were at offset 0, the next
    one would be at offset 1, the next at 2, and so
    on. The label aList would equal 0, the offset of
    the first letter.
  • We use data definition directives to allocate
    storage, based on the ff. predefined types

5
DATA DEFINITION DIRECTIVES
6
Define Byte (DB)
  • DB directive allocates storage for one or more
    8-bit values.
  • Syntax name DB initialvalue ,initialvalue
  • name is optional
  • initialvalue can be one or more 8-bit numeric
    values, a string constant, a constant expression,
    or a ?.
  • Examples
  • char db A ASCII character
  • signed1 db -128 smallest signed value
  • signed2 db 127 largest signed value

7
DB
  • unsigned1 db 0 smallest unsigned value
  • Unsigned2 db 255 largest unsigned value
  • Multiple values.
  • A list of 8-bit numbers may be grouped under a
    single label, with the values separated by
    commas.
  • list db 10,20,30,40
  • Characters and integers are one and the same, as
    far as the assembler is concerned.

8
DB
  • The following variables contain exactly the same
    value and may be processed the same way
  • char db A a character (ASCII 41h)
  • hex db 41h hexadecimal
  • dec db 65 decimal
  • bin db 01000001b binary
  • oct db 101q octal

9
DB
  • Each constant may use

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