What is assembler?

11

I always read things related to the assembler and I get confused. At first, I thought this was a mess and that assembler was the same thing as assembly. But it does not look like this.

What is assembler? And what's your relationship with assembly?

    
asked by anonymous 20.01.2017 / 17:10

4 answers

11

Assembler

Assembler, as its name says, is a assembler , and not a compiler strong>, even though it works very similarly. It takes text that is a programming code and transforms into binary code (machine code). What differs from a compiler is that the statements in the language have a one-to-one relationship in the binary code. The analysis and transformation of the code in an assembler is much simpler than a compiler.

Assembly

The low-level programming language is Assembly , which is assembled for an assembler (suffix er in English is equal to our pain and indicates an agent for an action). We use capitalization as a proper name.

There are several dialects for each physical architecture (x86, ARM, MIPS, etc.), or virtual (JVM, CIL, and many dynamic languages have their own Assembly). The semantics depend on the architecture, but the syntax does not and the specific assembler can adopt whatever one prefers.

Some people confuse machine code and Assembly. The first is binary, the Assembly is at a level that humans (normal) understand. They are mnemonics that define the instructions that the processor should execute.

Assembly is all imperative, and each mnemonic is a very simple statement manipulating a value in registers, moving the data between the registers and memory and controlling the execution flow.

When the person says he will program in assembler he makes the same mistake as the person who says he will program in Visual Studio.

assembly

But assembly in lowercase is that file with binary code of the CLR. p>

Examples

Code sample Assembly x86-64 syntax Intel:

; Assembler (x86) version of 99 Bottles of beer
; 
; This version is for NASM compiler but doesn't use any 
; macros, just all basic instructions for x86 assembler.
; Also only putchar() function is used to print character
; onto screen, and the whole rest is in code.
;
; nasm -fwin32 99.asm
; gcc -o 99.exe 99.obj

        global  _main
        extern  _putchar

        segment .data

_line_1_1        db ' bottles of beer on the wall, ', 0
_line_1_2        db ' bottles of beer.', 13, 10, 0
_line_2_1        db 'Take one down and pass it around, ', 0
_line_2_2        db ' bottles of beer on the wall.', 13, 10, 13, 10, 0
_line_2_2_one    db ' bottle of beer on the wall.', 13, 10, 13, 10, 0
_ending_lines    db '1 bottle of beer on the wall, 1 bottle of beer.', 13, 10
                 db 'Take one down and pass it around, no more bottles of beer on the wall.', 13,
10, 13, 10
                 db 'No more bottles of beer on the wall, no more bottles of beer. ', 13, 10
                 db 'Go to the store and buy some more, 99 bottles of beer on the wall.', 13, 10, 0

        segment .text

; this function converts integer in range 0-99 to string
_integer_to_string:
        mov     eax, dword [esp + 08h]    ; get the vavlue
        mov     ecx, 10                   ; 
        sub     edx, edx                  
        div     ecx                       ; divide it by 10
        mov     ecx, dword [esp + 04h]    ; get the output offset
        test    eax, eax                  ; is greater than 9
        jz      .skip_first_digit         ; skip saving 0 char if no
        add     al, 030h                  ; convert number to ascii char
        mov     byte [ecx], al            ; save
        inc     ecx                       ; increase pointer
        jmp     .dont_test_second_digit   ; 
     .skip_first_digit:                   ; only if less then 10
        test    edx, edx
        jz      .skip_second_digit
     .dont_test_second_digit:             ; if it was greater than 10
        add     dl, 030h                  ; than second digit must by 
        mov     byte [ecx], dl            ; written at no condition
        inc     ecx                     
     .skip_second_digit:                  ; only skip if value was 0
        mov     byte [ecx], ah            ; save the null ending char
        retn    4                         ; ret and restore stack
; function prints null-terminated line to stdout
_show_line:
        push    edi                       ; function save registers
        push    esi
        mov     edi, dword [esp + 0Ch]    ; get the pointer to string
        sub     eax, eax                  ; look for zeros
        sub     ecx, ecx                        
        dec     ecx                       ; set ecx to -1
        repnz   scasb                     ; search for 0 in string
        neg     ecx
        sub     ecx, 2                    ; get the string length w/o zero
        mov     esi, dword [esp + 0Ch]    ; get pointer once again
     .putchar_loop:
        push    ecx                       ; keep the counter
        lodsb                             ; get the char
        push    eax                       
        call    _putchar                  ; print char to stdout
        add     esp, 4                    ; correct stack 
        pop     ecx                       ; get back the counter
        dec     ecx                     
        jnz     .putchar_loop             ; if not last char then get next
        pop     esi                       ; restore registers
        pop     edi
        retn    4
; prints string for only one number
_bottles:
        push    ebp                       ; keep the offset to call params
        mov     ebp, esp
        sub     esp, 4                    ; reserve one local variable
        mov     eax, dword [ebp + 08h]    ; get number of bottles
        dec     eax                       ; is it 1?
        jnz     .more_than_one            ; nope, it's not
        push    _ending_lines             ; print the last lines
        call    _show_line
        jmp     .end                      ; exit function
     .more_than_one:
        inc     eax                       ; get the original value
        push    eax                       ; convert it to string
        lea     eax, [ebp - 04h]
        push    eax                       ; string will be stored here
        call    _integer_to_string
        lea     eax, [ebp - 04h]
        push    eax
        call    _show_line                ; 'xx'
        push    _line_1_1
        call    _show_line                ; ' bottles of beer on the wall, '
        lea     eax, [ebp - 04h]
        push    eax
        call    _show_line                ; 'xx'
        push    _line_1_2
        call    _show_line                ; ' bottles of beer.'
        mov     eax, dword [ebp + 08h]
        dec     eax                       ; in second line the value is one less
        push    eax
        lea     eax, [ebp - 04h]
        push    eax
        call    _integer_to_string        ; convert it to string
        push    _line_2_1
        call    _show_line                ; 'Take one down and pass it around, '
        lea     eax, [ebp - 04h]
        push    eax
        call    _show_line                ; 'xx'
        cmp     dword [ebp + 08h], 2
        jnz     .second_line_for_more_than_one
        push    _line_2_2_one             ; ' bottle of beer on the wall.'
        jmp     .show_line
     .second_line_for_more_than_one:   
        push    _line_2_2                 ; ' bottles of beer on the wall.'
     .show_line:
        call    _show_line
     .end:
        leave
        retn    4
; main function, the command line arguments are not important
_main:        
        pushad
        mov     ecx, 99                   ; printf from 99
     .main_loop:
        push    ecx
        push    ecx
        call    _bottles                  ; print lines for this value
        pop     ecx
        loop    .main_loop                ; if still greater than zero
        popad
        sub     eax, eax                  ; That's all folks!
        retn

No ARM:

;99 Bottles of Beer generator
;For ARM processors running RISCOS
;Using built in BASIC assembler
;

MOV R7, #99              ;bottle count kept in R7
MOV R12, R14             ;store caller return address

.beginverse              ;(_prints verses then returns to caller_)
BL  bottlesofbeer
ADR R0, onthewall
SWI "OS_Write0"          ;prints string at address in R0
BL  bottlesofbeer
SWI "OS_NewLine"
ADR R0, take
SWI "OS_Write0"
SUBS R7,R7,#1            
BLNE bottlesofbeer       ;beer left
BLEQ nobeer              ;no beer left
ADR R0, onthewall
SWI "OS_Write0"
SWI "OS_NewLine"
SWI "OS_NewLine"
BNE beginverse           ;go again if there's beer left
BL buymorebeer           ;print last verse
MOV PC, R12              ;exit to caller

.bottlesofbeer           ;(_prints "x bottle(s) of beer"_)
MOV R0, R7               ;arg1- number of bottles
ADR R1, bottlenum        ;arg2- buffer address
MOV R2, #3               ;arg3- buffer length
SWI "OS_ConvertInteger3" ;convert number of beers to string
SWI "OS_Write0"          ;and print it
CMP R7, #1             
ADR R0, bottles          ;
ADREQ R0, bottle         ;bottles is replaced with bottle if 1 bottle left
SWI "OS_Write0"
ADR R0, ofbeer
SWI "OS_Write0"
CMP R1, #0               ;unset zero flag so "nobeer" doesnt execute after return
MOV PC, R14              ;return

.buymorebeer             ;(_prints final verse_)
MOV R11, R14             ;save return address
BL nobeer
ADR R0, onthewall
SWI "OS_Write0"
ADR R0, comma
SWI "OS_Write0"
BL nobeer
SWI "OS_NewLine"
ADR R0, gotostore
SWI "OS_Write0"
MOV PC, R11              ;return to saved address

.nobeer                  ;(_prints "no more bottles of beer"_)
ADR R0, nomore
SWI "OS_Write0"
ADR R0, bottles
SWI "OS_Write0"
ADR R0, ofbeer
SWI "OS_Write0"
MOV PC, R14

;string components

.ofbeer
EQUS "of beer"           ;string contents
EQUB 0                   ;zero terminator

.onthewall
EQUS " on the wall "
EQUB 0

.bottle
EQUS " bottle "
EQUB 0

.bottles
EQUS " bottles "
EQUB 0

.take
EQUS "Take one down and pass it around, "
EQUB 0

.nomore
EQUS "no more"
EQUB 0

.bottlenum
EQUS "  "
EQUB 0

.comma
EQUS ","
EQUB 0

.gotostore
EQUS "Go to the store and buy some more...99 bottles of beer."
EQUB 0

Font .

Related questions:

20.01.2017 / 17:15
7
  

What is assembler? And what's your relationship with assembly?

Assembler is a compiler. It converts code written in the language Assembly to native code.

    
20.01.2017 / 17:13
6

Assembly is a programming language. But it is not a typical language. It is characterized by being a low-level programming language composed of simple and rigid format instructions that do not allow substructures and some labels (labels that are targets for deviation instructions). It normally (almost always, but there are some cases that do not) is mapped from one to one in instructions to be executed by the processor (a processor statement = an instruction in assembly).

Each statement is defined by a mnemonic. For example, in a statement MOV eax, 1 or JN algum_label , MOV and JN are mnemonics, which are also those that define the name of the statement in question.

The assembler is the program that converts the assembly code to the instructions themselves (encoded as a sequence of bytes). That is, the assembler is the compiler. However, the confusion between the assembly and assembler terms is common, and a lot of people talk about " programming in assembler" when in fact it should be " programming in assembly ".

Since there are several types of processors, each with its instruction set, this means that for each processor we have at least one assembly dialect. Different assembler developers may use different notations or different mnemonics for instructions, and therefore, even in the same architecture, there may be several distinct assembly dialects.

    
20.01.2017 / 17:16
2

I found this definition quite relevant:

Machine Code:

This is the output compiled by an assembler compiler.

Assembly

The readable form of machine code.

Assembly Language

Refers to a specific machine code language with x86 assembly.

Assembler

This is the tool used to compile source code into machine code.

Assembler Language

This is the language used by any assembler assembler.

I translated from that SO-en , I found it good, but my English is not the "the best" ...

    
20.01.2017 / 18:23