Starting assembly language in assembler ?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 2761 Views - Last Post: 04 August 2018 - 12:11 PM

#16 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Re: Starting assembly language in assembler ?

Posted 28 April 2018 - 11:42 AM

Ok , Thanks
One last question

so this is assembly language programming under ms dos ?

Posted Image
Was This Post Helpful? 0
  • +
  • -

#17 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,107
  • Joined: 03-April 12

Re: Starting assembly language in assembler ?

Posted 28 April 2018 - 01:41 PM

I'm not expert in Intel assembly, but looks like code that MASM eats.
That code just only uses 16-bit (8086) "subset". using the registers as 32-bit registers, the register names have 'e' in front of them, like eax, and 'r' if 64-bit registers are used, like rax, instead of ax (16-bit register).
MASM handles 16- and 32-bit code, but I'm not sure of 64-bit code. It requires 64-bit masm that can handle all of them.

But you can get quite far with 32-bit assembler.

This post has been edited by turboscrew: 28 April 2018 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

#18 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Re: Starting assembly language in assembler ?

Posted 28 April 2018 - 10:30 PM

Thanks for the reply ,

OK , I have few options .
I can focus on MS DOS Assembly , Linux Assembly or Windows MASM32 Assembly right now


I managed to collect few notes and looks like this . Not sure where to go from here






Quote

label mnemonic operand(s) ; comment



1.Labels - In an assembly language program, a label is simply a name for an address
2.Data Declarations - DB DW DD DQ DT
3.Opcode - MOV, PUSH , POP , ADD , SUB , OR , AND , XOR , DEC , INC
4.Registers - EAX EBX ECX EDX CS DS SS ES FS GS ESI EDI EIP ESP EBP cf pf af zf sf tf if df of iopl nt rf vm ac vif vip id
5.Arithmetic operators - add , sub ,mul , div, and , or , xor
6.Control flow operators - call ,test , cmp , jmp ,je , jne ,jge, ja, jae , jl , jle , jbe , jo , jno je , jg , jl , jz , jnz , js , jns
7.Pointers - mov eax, dword [ebx] , mov ax, word [bx] ,mov al, byte [0]


Quote

General purpose registers

Accumulator - Input/Output , Operations
Base register - Holds address of data
Counter register - Counts , used in loops
Data register - Holds data for output



Segment registers

Code Segment - Holds address of code segment
Data Segment - Holds address of data segment
Stack segment - Holds address of stack segment
Extra segment - Holds address of data segment



Index registers

Source Index - Points the source operand
Destination Index -Points the destination operand


Special purpose registers

Instruction Pointer - Holds the next instruction
Stack Pointer - Points current top of the stack
Base pointer - Base of the top of the stack

Flag registers - Holds current status of the program




Quote

The three basic modes of addressing are −

Register addressing
Immediate addressing
Memory addressing

Register Addressing

In this addressing mode, a register contains the operand. Depending upon the instruction, the register may be the first operand, the second operand or both.

For example,

MOV DX, TAX_RATE ; Register in first operand
MOV COUNT, CX ; Register in second operand
MOV EAX, EBX ; Both the operands are in registers

Immediate Addressing

An immediate operand has a constant value or an expression. When an instruction with two operands uses immediate addressing, the first operand may be a register or memory location, and the second operand is an immediate constant. The first operand defines the length of the data.

For example,

BYTE_VALUE DB 150 ; A byte value is defined
WORD_VALUE DW 300 ; A word value is defined
ADD BYTE_VALUE, 65 ; An immediate operand 65 is added
MOV AX, 45H ; Immediate constant 45H is transferred to AX

Direct Memory Addressing

When operands are specified in memory addressing mode, direct access to main memory, usually to the data segment, is required. This way of addressing results in slower processing of data. To locate the exact location of data in memory, we need the segment start address, which is typically found in the DS register and an offset value. This offset value is also called effective address.

In direct addressing mode, the offset value is specified directly as part of the instruction, usually indicated by the variable name. The assembler calculates the offset value and maintains a symbol table, which stores the offset values of all the variables used in the program.

In direct memory addressing, one of the operands refers to a memory location and the other operand references a register.

For example,

ADD BYTE_VALUE, DL ; Adds the register in the memory location
MOV BX, WORD_VALUE ; Operand from the memory is added to register


Direct-Offset Addressing

This addressing mode uses the arithmetic operators to modify an address. For example, look at the following definitions that define tables of data −

BYTE_TABLE DB 14, 15, 22, 45 ; Tables of bytes
WORD_TABLE DW 134, 345, 564, 123 ; Tables of words

The following operations access data from the tables in the memory into registers −

MOV CL, BYTE_TABLE[2] ; Gets the 3rd element of the BYTE_TABLE
MOV CL, BYTE_TABLE + 2 ; Gets the 3rd element of the BYTE_TABLE
MOV CX, WORD_TABLE[3] ; Gets the 4th element of the WORD_TABLE
MOV CX, WORD_TABLE + 3 ; Gets the 4th element of the WORD_TABLE


Indirect Memory Addressing

This addressing mode utilizes the computer's ability of Segment:Offset addressing. Generally, the base registers EBX, EBP (or BX, BP) and the index registers (DI, SI), coded within square brackets for memory references, are used for this purpose.

Indirect addressing is generally used for variables containing several elements like, arrays. Starting address of the array is stored in, say, the EBX register.

The following code snippet shows how to access different elements of the variable.

MY_TABLE TIMES 10 DW 0 ; Allocates 10 words (2 bytes) each initialized to 0
MOV EBX, [MY_TABLE] ; Effective Address of MY_TABLE in EBX
MOV [EBX], 110 ; MY_TABLE[0] = 110
ADD EBX, 2 ; EBX = EBX +2
MOV [EBX], 123 ; MY_TABLE[1] = 123




MS DOS

.model small

.data
 max_temp DB 92h       
 min_temp DB 52h
 avg_temp DB ?
.code
.startup

 mov ax,@data          
 mov ds,ax
 mov al, max_temp      
 add al, min_temp      
 mov ah, 00h           
 adc ah, 00h           
 mov bl, 02h          
 div bl               
 mov avg_temp, al     
.exit
end


Linux

section .text

global _start               ;must be declared for using gcc

_start:                     ;tell linker entry point

    mov     eax, '3'
    sub     eax, '0'
    mov     ebx, '4'
    sub     ebx, '0'
    add     eax, ebx
    add     eax, '0'

    mov     [sum], eax

    mov     ecx, msg 
    mov     edx, len
    mov     ebx, 1          ;file descriptor (stdout)
    mov     eax, 4          ;system call number (sys_write)
    int     0x80            ;call kernel

    mov     ecx, sum

    mov     edx, 1
    mov     ebx, 1          ;file descriptor (stdout)
    mov     eax, 4          ;system call number (sys_write)
    int     0x80            ;call kernel

    mov     eax, 1          ;system call number (sys_exit)
    int     0x80            ;call kernel


section .data

    msg     db              "The sum is:", 0xA,0xD 

    len equ $ - msg   

    segment .bss

    sum resb 1


MASM32

.386
.model flat ,stdcall
OPTION CASEMAP:NONE

include masm32.inc
include kernel32.inc
includelib kernel32.lib
includelib masm32.lib

.DATA
MasgName db "Enter the NUmber   ",00ah,00dh

.DATA?
num db 2 dup (?)

.code
start:

invoke StdOut ,addr MasgName

; Get 1st number
invoke StdIn ,addr num,10
; eax = length
 mov [num+ eax-2 ], 0   ; Set terminator
invoke StripLF,ADDR num      
; Now convert ASCIIz number to decimal
invoke atodw , addr num
mov ebx, eax ; Move number from eax to ebx

;_____________
invoke StdOut ,addr MasgName
; Get 2nd number
invoke StdIn ,addr num,10
; eax = length
mov [num+ eax-2 ], 0  ; Set terminator
invoke StripLF,ADDR num    


; Now convert ASCIIz number to decimal
invoke atodw , addr num
        ; eax is 2nd number

add eax,ebx
         
invoke dwtoa, eax,addr num
invoke StdOut ,addr num
invoke StdIn ,addr num,10
invoke ExitProcess,0
end start

Was This Post Helpful? 0
  • +
  • -

#19 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,107
  • Joined: 03-April 12

Re: Starting assembly language in assembler ?

Posted 29 April 2018 - 05:07 AM

It all depends on which way you want to go.

MSDOS and Windows can both use MASM (yes, Windows' 'ml' is MASM). Between them it's basically about system calls. The basic assembly is the same.
Linux, on the other hand, uses mostly AT&T syntax and that's quite different from the MS-world assembly.

The MS-DOS uses BIOS/BDOS calls (int 21h), and the Windows uses Win32-calls (invoke). Linux uses Linux kernel system calls (int 0x80).

In school courses, they usually use MS-DOS, because the system calls are simpler. Easier for a beginner to learn.
The Windows-world uses the Windows (Wow strange! :-D ).
The Linux-world uses Linux (=AT&T syntax and gas + gdb).

This is your crossroads, but there is nothing keeping you from learning the other systems later.
Try asking yourself why do you want to learn assembly. That may give you the answer.

This post has been edited by turboscrew: 29 April 2018 - 05:44 AM

Was This Post Helpful? 1
  • +
  • -

#20 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,107
  • Joined: 03-April 12

Re: Starting assembly language in assembler ?

Posted 29 April 2018 - 05:34 AM

I'm not sure if this helps, but unless you want to advance on several fronts at the same time, you need to select between Linux-world and MS-world. I think the typical path in MS-world is to learn DOS-programming (works with command prompt) and then advance towards Win32.
Was This Post Helpful? 1
  • +
  • -

#21 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Re: Starting assembly language in assembler ?

Posted 29 April 2018 - 07:07 AM

Thanks a lot for the reply , I learned a lot of important things from your last post especially about windows system calls , invoke .

I think more examples are available on the internet for Linux

Maybe i should partition my HDD one more time and dual boot Windows 7 with some Linux version .

I was also thinking about learning reverse engineering , But that takes a lot of time

Last time i tried to learn reverse engineering an application called simplecppreverseme.exe , i got stuck with a lot of subroutines in the disassembler .

Then i had no idea what to do to these subroutines with a lot of data inside the registers and memory locations ,The only thing i could figure out was that the Instruction Pointer - Holds the next instruction ,Lol


Quote

label mnemonic operand(s) ; comment



1.Labels - In an assembly language program, a label is simply a name for an address
2.Data Declarations - DB DW DD DQ DT
3.Opcode - MOV, PUSH , POP , ADD , SUB , OR , AND , XOR , DEC , INC
4.Registers - EAX EBX ECX EDX CS DS SS ES FS GS ESI EDI EIP ESP EBP cf pf af zf sf tf if df of iopl nt rf vm ac vif vip id
5.Arithmetic operators - add , sub ,mul , div, and , or , xor
6.Control flow operators - call ,test , cmp , jmp ,je , jne ,jge, ja, jae , jl , jle , jbe , jo , jno je , jg , jl , jz , jnz , js , jns
7.Pointers - mov eax, dword [ebx] , mov ax, word [bx] ,mov al, byte [0]


Quote

The three basic modes of addressing are −

Register addressing
Immediate addressing
Memory addressing

Register Addressing

In this addressing mode, a register contains the operand. Depending upon the instruction, the register may be the first operand, the second operand or both.

For example,

MOV DX, TAX_RATE ; Register in first operand
MOV COUNT, CX ; Register in second operand
MOV EAX, EBX ; Both the operands are in registers

Immediate Addressing

An immediate operand has a constant value or an expression. When an instruction with two operands uses immediate addressing, the first operand may be a register or memory location, and the second operand is an immediate constant. The first operand defines the length of the data.

For example,

BYTE_VALUE DB 150 ; A byte value is defined
WORD_VALUE DW 300 ; A word value is defined
ADD BYTE_VALUE, 65 ; An immediate operand 65 is added
MOV AX, 45H ; Immediate constant 45H is transferred to AX

Direct Memory Addressing

When operands are specified in memory addressing mode, direct access to main memory, usually to the data segment, is required. This way of addressing results in slower processing of data. To locate the exact location of data in memory, we need the segment start address, which is typically found in the DS register and an offset value. This offset value is also called effective address.

In direct addressing mode, the offset value is specified directly as part of the instruction, usually indicated by the variable name. The assembler calculates the offset value and maintains a symbol table, which stores the offset values of all the variables used in the program.

In direct memory addressing, one of the operands refers to a memory location and the other operand references a register.

For example,

ADD BYTE_VALUE, DL ; Adds the register in the memory location
MOV BX, WORD_VALUE ; Operand from the memory is added to register


Direct-Offset Addressing

This addressing mode uses the arithmetic operators to modify an address. For example, look at the following definitions that define tables of data −

BYTE_TABLE DB 14, 15, 22, 45 ; Tables of bytes
WORD_TABLE DW 134, 345, 564, 123 ; Tables of words

The following operations access data from the tables in the memory into registers −

MOV CL, BYTE_TABLE[2] ; Gets the 3rd element of the BYTE_TABLE
MOV CL, BYTE_TABLE + 2 ; Gets the 3rd element of the BYTE_TABLE
MOV CX, WORD_TABLE[3] ; Gets the 4th element of the WORD_TABLE
MOV CX, WORD_TABLE + 3 ; Gets the 4th element of the WORD_TABLE


Indirect Memory Addressing

This addressing mode utilizes the computer's ability of Segment:Offset addressing. Generally, the base registers EBX, EBP (or BX, BP) and the index registers (DI, SI), coded within square brackets for memory references, are used for this purpose.

Indirect addressing is generally used for variables containing several elements like, arrays. Starting address of the array is stored in, say, the EBX register.

The following code snippet shows how to access different elements of the variable.

MY_TABLE TIMES 10 DW 0 ; Allocates 10 words (2 bytes) each initialized to 0
MOV EBX, [MY_TABLE] ; Effective Address of MY_TABLE in EBX
MOV [EBX], 110 ; MY_TABLE[0] = 110
ADD EBX, 2 ; EBX = EBX +2
MOV [EBX], 123 ; MY_TABLE[1] = 123




Quote

General purpose registers

Accumulator - Input/Output , Operations
Base register - Holds address of data
Counter register - Counts , used in loops
Data register - Holds data for output



Segment registers

Code Segment - Holds address of code segment
Data Segment - Holds address of data segment
Stack segment - Holds address of stack segment
Extra segment - Holds address of data segment



Index registers

Source Index - Points the source operand
Destination Index -Points the destination operand


Special purpose registers

Instruction Pointer - Holds the next instruction
Stack Pointer - Points current top of the stack
Base pointer - Base of the top of the stack

Flag registers - Holds current status of the program

Was This Post Helpful? 0
  • +
  • -

#22 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 4,366
  • Joined: 21-June 11

Re: Starting assembly language in assembler ?

Posted 29 April 2018 - 07:33 AM

View Postturboscrew, on 29 April 2018 - 02:07 PM, said:

The Linux-world uses Linux (=AT&T syntax and gas + gdb).


Is gas really used widely for hand-written assembly? I was under the impression that nasm is more common for that (but I don't really know).
Was This Post Helpful? 0
  • +
  • -

#23 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,107
  • Joined: 03-April 12

Re: Starting assembly language in assembler ?

Posted 30 April 2018 - 02:29 PM

View Postsepp2k, on 29 April 2018 - 05:33 PM, said:

View Postturboscrew, on 29 April 2018 - 02:07 PM, said:

The Linux-world uses Linux (=AT&T syntax and gas + gdb).


Is gas really used widely for hand-written assembly? I was under the impression that nasm is more common for that (but I don't really know).


Could be, but AT&T is the "native" way. C compiler produces AT&T and assembly code for Linux kernel is AT&T.
Like https://elixir.bootl...el/verify_cpu.S
Also, AT&T is kind of "GNU-native" too.

There are some benefits in using gas for Linux/Unix: It was originally, and still is, the system assembler, so it's as compatible with the system as can be, and it supports several architectures - they all have, of course, their own mnemonics, but common directives. Unix was originally developed in AT&T, and that may explain some things... Even today, there are two Unix versions: AT&T and Berkeley (BSD). Only those have right to the name "Unix".

This post has been edited by turboscrew: 30 April 2018 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#24 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Re: Starting assembly language in assembler ?

Posted 03 May 2018 - 10:31 AM

Can somebody please explain to me what is this below the second circle ?

Posted Image
Was This Post Helpful? 0
  • +
  • -

#25 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 4,366
  • Joined: 21-June 11

Re: Starting assembly language in assembler ?

Posted 03 May 2018 - 11:10 AM

The second column contains the machine code for each instruction in hexadecimal notation. Is that what you wanted to know?
Was This Post Helpful? 0
  • +
  • -

#26 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Re: Starting assembly language in assembler ?

Posted 03 May 2018 - 11:12 AM

Is it same as the third column , but in HEX right ?
Was This Post Helpful? 0
  • +
  • -

#27 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 4,366
  • Joined: 21-June 11

Re: Starting assembly language in assembler ?

Posted 03 May 2018 - 11:17 AM

Yes, that's right.
Was This Post Helpful? 0
  • +
  • -

#28 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Re: Starting assembly language in assembler ?

Posted 03 May 2018 - 11:34 AM

That is a bit confusing , i wonder why that part in HEX is there for ?
Was This Post Helpful? 0
  • +
  • -

#29 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,107
  • Joined: 03-April 12

Re: Starting assembly language in assembler ?

Posted 06 May 2018 - 05:10 AM

Well, it's the "raw" machine code. It has its uses, although quite seldom needed.
Was This Post Helpful? 0
  • +
  • -

#30 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Re: Starting assembly language in assembler ?

Posted 06 May 2018 - 07:35 PM

Thanks a lot for all the reply turboscrew
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3