This blog includes subject like Computer Organization, Microprocessor, Digital Electronics, System Programming

Pages

This blog includes subject like Computer Organization, Microprocessor, Digital Electronics, System Programming

Powered by Blogger.

Friday, May 10, 2019

Assembly Language Basic Part-2



What is Assembly Language?
l  Every PC has a microprocessor that manages computer's ALU and control activities.
l  Each processor has its own set of instructions for handling various operations like Input and output.
l  To operate this activity assembly language is used.

Why Study Assembly Language?
l  It is used to Interface of programs with OS, processor and BIOS.
l  Representation of data in memory and other external devices.
l  It requires less memory and execution time.
l  It allows hardware-specific complex jobs in an easier way.

Assembler
l  Assembler is a type of converter accept assembly language as input and convert it into equivalent machine language.
l  There are following types of popular assembler:
1. Netwide Assembler (NASM)
2. Microsoft Assembler (MASM)
3. Borland Turbo Assembler (TASM)

Assembly Basic Syntax
An assembly program can be divided into three sections:
1. The data section
2. The bss (Block Started Symbol) section
3. The text section
1. The Data Section
l  The data section is used for declaring initialized data or constants.
l  This data does not change at runtime.
l  The syntax for declaring data section is:
Syntax:   section .data
2. The bss (Block Started Symbol) section:
l  The bss section is used for declaring variables.
l  These variables are accessed from keyboard and used to display result.
l  The syntax for declaring bss section is:
Syntax: section .bss
3. The text section
l  The text section is used for keeping the actual code.
l  This section must begin with the declaration global  _start:
l  Which tells the kernel where the program execution begins.
l  The syntax for declaring text section is:
section .text
global  _start:
_start:

Syntax of Assembly Language Statements:
l  Assembly language statements are entered one statement per line. Each statement follows the following format:
l  [label]   mnemonic          [operands]         [;comment]
l  e.g.
abc1:                     mov                       ax, 00h                  ;initialize acc.


The Hello World Program!!!
section .data
msg db 'Hello, world!', 10 ;our string, 10=\n like C.
len equ $ - msg                                                 ;length of our dear string

section .text
global _start                       ;must be declared for linker (ld)
_start:                                                   ;tells linker entry point
mov edx,len                                       ;message length
mov ecx,msg                     ;message to write
mov ebx,1                                           ;file descriptor (stdout)
mov eax,4                                           ;system call number (sys_write)
int 80h                                                  ;call kernel
mov eax,1           ;system call number (sys_exit)
int 80h                                  ;call kernel

Compile and execute Program
>nasm -f elf64 progname.asm   ; Compile code
>ld -o progname progname.o    ;Load & create obj
>./progname     ;Execute program
               




Assembly Languages Introduction-2
Different Registers
l  Data Register


64-bit data register
l  RAX
l  RBX
l  RCX
l  RDX

Pointer Registers

Index Register



System Call in 32-bit
l  Exit system call:
mov  eax,1 ; system call number (sys_exit)
Int  80h    ; call kernel
l  Write System call:
mov       edx,4                     ;message length
mov       ecx,msg               ;message to write
mov       ebx,1                     ;file descriptor (stdout)
mov       eax,4                     ;system call number (sys_write)
int           80h                         ;call kernel

System Call 32-bit
Name

%eax

sys_read
0
sys_write
1
sys_open
2
sys_close
3
sys_exit
60



Data Section Data Type
Type Specified
Bytes addressed
DB-Define Byte
1
DW- Define Word
2
DD- Double Word
4
QD- Quad Word
8
TB- Tera Byte
10

BSS Section Data Type
Type Specified
Bytes addressed
RESB- Reserved Byte
1
RESW- Reserved Word
2
RESD- Reserve Double Word
4
RESQ- Reserve Quad Word
8
REST- Reserve Tera Byte
10

Macros
l  To avoid repetative code of block in program macros are used.
l  The Syntax for macro definition:
%macro macro_name  number_of_params
<macro body>
%endmacro

Read Macro 64-bit
%macro read 2
    mov    rax,0      ;read
    mov    rdi,0       ;stdin/keyboard
    mov    rsi,%1    ;buf
    mov    rdx,%2    ;buf_len
    syscall
%endmacro

Write Macro 64-bit
%macro display 2
    mov    rax,1      ;print
    mov    rdi,1       ;stdout/screen
    mov    rsi,%1    ;msg
    mov    rdx,%2    ;msg_len
    syscall
%endmacro

Array in Assembly
l  The data definition directives can also be used for defining a one dimensional array.
l  Let us define a one dimensional array of numbers.
Ex.1 NUMBERS DW 34, 45, 56, 67,75, 89
Ex.2 Arr_Name TIMES 8 DW 0

Loop in Assembly
The JMP instruction can be used for implementing loops. For example, the following code snippet can be used for executing the loop-body 10 times.
MOV  CL, 10
L1:
<LOOP-BODY>
DEC CL
JNZ L1
The processor instruction set however includes a group of loop instructions for implementing iteration.


Procedure
l  Procedures or subroutines are very important in assembly language, as the assembly language programs tend to be large in size.
l  Procedures are identified by a name.
l  Syntax:
proc_name:
procedure body
...
Ret
l  The procedure is called from another function by using the CALL instruction. The CALL instruction should have
l  The name of the called procedure as argument as shown below:
Syntax: CALL proc_name

Display Procedure in NASM
;display procedure for 64bit
display:                                                                 ;name of procedure, similar to label
        mov rsi,char_answer+15                      ;Point the RSI to the last position of char_answer variable,like array
        mov rcx,16                                                  ;RCX=16 will act as counter for 16 digits result
        cnt:    mov rdx,0                                         ;cnt=label, For division of RDX and RAX make first RDX=0
                mov rbx,16h       ;RBX=16, RBX is the divisor. 16 in hex = 10 decimal & getlast digit reminder
                div rbx                    ;Divide RbX,RAX by RBX
                cmp dl,09h             ;Remainder comes in DL register, compare it with 9
                jbe add30               ;If DL<=9, jump to label add30
                add dl,07h              ;add 7 to DL.difference from 9 to F.
        add30:    add dl,30h       ;label add30, add 30 to dl, means dl=dl+30 ASCII
                mov [rsi],dl              ;move the value in DL to the address at which RSI
                dec rsi                     ;decrement the address in RSI,pointing to prev.loc.
                dec rcx                                   ;decrement count in RCX
                jnz cnt                                   ;jump to cnt lab, till zero flag is not set
        scall 1,1,char_answer,16       ;call scall macro to display answer
ret                                            ;return statement


Assembly Language Basic Part-1



What is Assembly Language?
l  Every PC has a microprocessor that manages computer's ALU and control activities.
l  Each processor has its own set of instructions for handling various operations like Input and output.
l  To operate this activity assembly language is used.

Why Study Assembly Language?
l  It is used to Interface of programs with OS, processor and BIOS.
l  Representation of data in memory and other external devices.
l  It requires less memory and execution time.
l  It allows hardware-specific complex jobs in an easier way.

Assembler
l  Assembler is a type of converter accept assembly language as input and convert it into equivalent machine language.
l  There are following types of popular assembler:
1. Netwide Assembler (NASM)
2. Microsoft Assembler (MASM)
3. Borland Turbo Assembler (TASM)

Assembly Basic Syntax
An assembly program can be divided into three sections:
1. The data section
2. The bss (Block Started Symbol) section
3. The text section
1. The Data Section
l  The data section is used for declaring initialized data or constants.
l  This data does not change at runtime.
l  The syntax for declaring data section is:
Syntax:   section .data
2. The bss (Block Started Symbol) section:
l  The bss section is used for declaring variables.
l  These variables are accessed from keyboard and used to display result.
l  The syntax for declaring bss section is:
Syntax: section .bss
3. The text section
l  The text section is used for keeping the actual code.
l  This section must begin with the declaration global  _start:
l  Which tells the kernel where the program execution begins.
l  The syntax for declaring text section is:
section .text
global  _start:
_start:

Syntax of Assembly Language Statements:
l  Assembly language statements are entered one statement per line. Each statement follows the following format:
l  [label]   mnemonic          [operands]         [;comment]
l  e.g.
abc1: mov   ax, 00h                  ;initialize acc.


The Hello World Program!!!
section .data
msg db 'Hello, world!', 10 ;our string, 10=\n like C.
len equ $ - msg                  ;length of our dear string

section .text
global _start                       ;must be declared for linker (ld)
_start:                                 ;tells linker entry point
mov edx,len                       ;message length
mov ecx,msg                     ;message to write
mov ebx,1                         ;file descriptor (stdout)
mov eax,4                         ;system call number (sys_write)
int 80h                              ;call kernel
mov eax,1                         ;system call number (sys_exit)
int 80h                              ;call kernel

Compile and execute Program
>nasm -f elf64 progname.asm   ; Compile code
>ld -o progname progname.o    ;Load & create obj
>./progname     ;Execute program