writing .asm code to create output data from a 2d Array

79 views Asked by At

I am attempting to write code which can help to create grades for students provided two separate arrays using MASM. This is what I have so far:

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
    ; Define a 2 arrays
    name_data    dbnum  90,85,78,90,88,76,95,89,82,91
                 dbname "Dan","Frank","Bob","Alice","John","Eva", "Frank","Grace","Henry","Ivy"

    ; Number of names
    num_names equ 10

.code

_start:
    ; Initialize registers
    mov ecx, num_names   ;
    mov esi, name_data   ; 


init_table:
    mov edi, ecx            ; 

    mov eax, [esi]
    mov [name_data+ edi], eax

    add esi, 2  ; Assuming each name is word

    mov eax, [esi]
    mov [name_data+ edi + 4], eax

    add esi, 2  ; Assuming each score is word


    loop init_table

    section .text


    mov ecx, num_names
    mov esi, name_data

selection_sort:

    mov edx, ecx


    mov edi, ecx
    imul edi, 2

    mov ebx, [name_data+ edi + 2]  

    inner_loop:
        cmp ebx, [name_data+ edx * 2 + 2]

        jg update_max_index

    inner_loop_done:
        sub edx, 1


        jnz inner_loop


    update_max_index:
        mov eax, ecx
        imul eax, 2

        ; Swap names
        mov esi, [name_data+ eax]
        mov [name_data+ eax], [name_data+ edx * 2]
        mov [name_data+ edx * 2], esi

        ; Swap grades
        mov eax, [name_data+ eax + 2]
        mov [name_data+ edx * 2 + 2], eax
        mov [name_data+ edi + 2], ebx


    sub ecx, 1

    jnz selection_sort

    mov ecx, num_names
    mov esi, name_data

assign_score:
    mov edi, ecx
    imul edi, 2

    mov eax, [name_data+ edi + 4]

    cmp eax, 90
    jge assign_score_1

    cmp eax, 80
    jge assign_score_2

    cmp eax, 70
    jge assign_score_3

    cmp eax, 60
    jge assign_score_4

    jmp assign_score_5

assign_score_1:
    mov byte [name_data+ edi + 8], '1'
    jmp next_iteration

assign_score_2:
    mov byte [name_data+ edi + 8], '2'
    jmp next_iteration

assign_score_3:
    mov byte [name_data+ edi + 8], '3'
    jmp next_iteration

assign_score_4:
    mov byte [name_data+ edi + 8], '4'
    jmp next_iteration

assign_score_5:
    mov byte [name_data+ edi + 8], '5'

next_iteration:

    sub ecx, 1


    jnz assign_score


    mov eax, 0  ; Count of 1's
    mov ebx, 0  ; Count of 2's
    mov ecx, 0  ; Count of 3's
    mov edx, 0  ; Count of 4's
    mov esi, 0  ; Count of 5's


    mov ecx, num_names
    mov esi, name_data

count_score:

    mov edi, ecx
    imul edi, 2


    mov al, [name_data+ edi + 8]


    cmp al, '1'
    je increment_1

    cmp al, '2'
    je increment_2

    cmp al, '3'
    je increment_3

    cmp al, '4'
    je increment_4

    cmp al, '5'
    je increment_5

    jmp next_iteration_count

increment_1:
    inc eax
    jmp next_iteration_count

increment_2:
    inc ebx
    jmp next_iteration_count

increment_3:
    inc ecx
    jmp next_iteration_count

increment_4:
    inc edx
    jmp next_iteration_count

increment_5:
    inc esi

next_iteration_count:

    sub ecx, 1


    jnz count_score


    mov edi, eax  
    mov eax, ebx 
    mov ebx, ecx  
    mov ecx, edx 
    mov edx, esi  


    mov rax, 1     
    mov rdi, 1     
    mov rsi, rsp    
    mov rdx, 5      
    syscall


end

When I run this code in visual studio I get these errors which I have been unable to fix after attempting to adjust the syntax on lines 13-14 and also attempting to close the program with an end main key word:

Error   A2008   syntax error : in instruction   MASM2   C:\Users\Diane\source\repos\MASM2\MASM2\MASMTest.asm    13  
Error   A2008   syntax error : in instruction   MASM2   C:\Users\Diane\source\repos\MASM2\MASM2\MASMTest.asm    14  
Error   A1010   unmatched block nesting : main  MASM2   C:\Users\Diane\source\repos\MASM2\MASM2\MASMTest.asm    16  
Error   MSB3721 The command "ml.exe /c /nologo /Zi /Fo"Debug\MASMTest.obj" /W3 /errorReport:prompt  /TaMASMTest.asm" exited with code 1.    MASM2   C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\BuildCustomizations\masm.targets  70  ```



I was expecting to have sorted score data and then assign a secondary score to the original score categorizing them into scores of 1-5. I think I may be running into an issue with how I'm designing my loops. 
0

There are 0 answers