Skip to content

Latest commit

 

History

History
142 lines (129 loc) · 2.6 KB

README.md

File metadata and controls

142 lines (129 loc) · 2.6 KB

eCompiler

Basic TS Syntax to ASM 8086 translator

screenshot of finished program

code (TS like)

{
    var i: number;
    var arr: number[5];
    var sum: number;
    var count: number;

    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;
    arr[3] = 40;
    arr[4] = 50;

    write("Что сделать с введенными переменными?: 1. Сложить 2. Вычесть");
    read(i);

    sum = 0;
    count = 0;

    while (count < 5) {
        if (i == 1) {
            sum = sum + arr[count];
        } else if (i == 2) {
            sum = sum - arr[count];
        } else {
            write("Некорректный ввод!");
            break;
        }

        count = count + 1;
    }

    write("Сумма равна: ");
    write(sum);
}

code (ASM i386)

; asm file generated by eCompiler from ../data/main.ts
section .text
    global main
    extern printf
    extern scanf
    
section .bss    ; uninitialized variables
    i resb 4
    sum resb 4
    count resb 4
    t1 resb 4
    t2 resb 4
    t3 resb 4
    t4 resb 4
    t5 resb 4
    t6 resb 4
    t7 resb 4
    arr resb 20
section .data    ; strings and other big stuff


    line0 db 'Что сделать с введенными переменными?: 1. Сложить 2. Вычесть'
    lenLine0 equ $-line0
    line1 db 'Некорректный ввод!'
    lenLine1 equ $-line1
    line2 db 'Сумма равна: '
    lenLine2 equ $-line2
    fmtd db '%d', 10, 0
    fmtf db '%f', 10, 0
    fmtc db '%c', 10, 0
    fmtb db '%d', 10, 0
    fmtdi db '%d', 0
    fmtci db '%c', 0
    fmtbi db '%d', 0
    
main:
L1:
    mov eax, 0
    mov ebx, 4
    imul eax, ebx
    mov [t1], eax
    mov eax, 10
    mov ebx, [t1]
    mov dword [arr + ebx * 4], eax
L3:
    mov eax, 1
    mov ebx, 4
    imul eax, ebx
    mov [t2], eax
    mov eax, 20
    mov ebx, [t2]
    mov dword [arr + ebx * 4], eax
L4:
.......
L17:
    mov eax, [count]
    mov ebx, 4
    imul eax, ebx
    mov [t7], eax
    mov eax, [sum]
    mov ecx, [t7]
    mov ebx, dword [arr + ecx * 4]
    sub eax, ebx
    mov [sum], eax
    jmp L14
L18:
    mov eax, 4
    mov ebx, 1
    mov ecx, line1
    mov edx, lenLine1
    int 80h
L19:
    jmp L12
L14:
    mov eax, [count]
    mov ebx, 1
    add eax, ebx
    mov [count], eax
    jmp L11
L12:
    mov eax, 4
    mov ebx, 1
    mov ecx, line2
    mov edx, lenLine2
    int 80h
L20:
    mov eax, [sum]
    push eax
    push fmtd
    call printf
    add esp, 8
L2:
    mov eax, 1
    mov ebx, 0
    int 80h