0

I need to create a simple x96-64 assembly language program to compute the min, middle value, max, sum and integer average of a list of numbers. When I try to assemble it gives me errors. My code so far:

; -----
;  Define constants.

NULL        equ 0           ; end of string

TRUE        equ 1
FALSE       equ 0

EXIT_SUCCESS    equ 0           ; successful operation
SYS_exit    equ 60          ; call code for terminate

; -----

lst     dd   4220, -1116,  1542,  1240,  1677
    dd  -1635,  2426,  1820,  1246,  -333
    dd   2315,  -215,  2726,  1140,  2565
    dd   2871,  1614,  2418,  2513,  1422
    dd   -119,  1215, -1525,  -712,  1441
    dd  -3622,  -731, -1729,  1615,  2724
    dd   1217,  -224,  1580,  1147,  2324
    dd   1425,  1816,  1262, -2718,  1192
        dd  -1435,   235,  2764, -1615,  1310
    dd   1765,  1954,  -967,  1515,  1556
    dd   1342,  7321,  1556,  2727,  1227
    dd  -1927,  1382,  1465,  3955,  1435
    dd   -225, -2419, -2534, -1345,  2467
    dd   1615,  1961,  1335,  2856,  2553
        dd  -1035,  1835,  1464,  1915, -1810
    dd   1465,  1554,  -267,  1615,  1656
    dd   2192,  -825,  1925,  2312,  1725
    dd  -2517,  1498,  -670,  1475,  2030
    dd   1223,  1883, -1173,  1350,  2415
    dd   -335,  1125,  1118,  1713,  3020
length      dd  100

lstMin      dd  0
lstMid      dd  0
lstMax      dd  0
lstSum      dd  0
lstAve      dd  0

evenCnt     dd  0
evenSum     dd  0
evenAve     dd  0

tenCnt      dd  0
tenSum      dd  0
tenAve      dd  0

; *****************************************************************

section .text
global _start
_start:

; ----------------------------------------------
mov rcx, 0
mov ecx, dword [length]
mov eax, dword[lst]
mov dword, [lst +lstMin] eax
mov dword, [lst +lstMax] eax
mov rsi, 0
mov dword [lst +lstSum],0


sumLp:
    mov eax, dword [lst+rsi]
    add dword [lst + lstSum],eax
    cmp eax, dword [lst+lstMin]
    jge minDone
    mov dword[lstMin],eax
minDone:
    cmp eax,dword [lstMax]
    jle maxDone
    mov dword[lstMax],eax
maxDone:
    add rsi, 4
    dec rcx
    cmp rcx, 0
    jne sumLp
    mov eax, dword [lstSum]
    cdq
    idiv dword [length]
    mov dword[lstAve],eax

; *****************************************************************
;   Done, terminate program.

last:
mov eax, SYS_exit       ; call call for exit (SYS_exit)
mov ebx, EXIT_SUCCESS   ; return code of 0 (no error)
syscall

The errors I get when assembling with YASM are similar to:

myprog.asm:60: error: unexpected `,' after instruction
myprog.asm:61: error: unexpected `,' after instruction

Why am I getting these errors and how can I fix them?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    Why are you adding `lst` in each of the pointer accesses? For example `mov dword, [lst +lstMin] eax` should probably be `mov dword [lstMin], eax` . You also have an extra comma after `dword`. you'll need to make similar fixes where you add `lst` inappropriately. – Michael Petch Jan 31 '18 at 21:51
  • the error is effective address to complex – Denis Dimitrov Jan 31 '18 at 22:02
  • 1
    You should also place a `section .data` directive above the `lst` data so that it gets placed in the data section and not some other section (you don't want it in a page that might not be wriiteable) – Michael Petch Jan 31 '18 at 22:03
  • 1
    so I fixed the comma errors and removed where I added lst in each one, it took care of the errors when trying to assemble so I am now able to open it in the debugger, I guess I'm just having trouble understanding the logic of the loop and how it adds all the numbers together to store the result in the sum – Denis Dimitrov Jan 31 '18 at 22:10
  • 1
    crap completely forgot about the section.data, thank you so much – Denis Dimitrov Jan 31 '18 at 22:12
  • 1
    You're going to need to (partially) sort to get the median, if that's what you mean by "middle". min/max/sum (and thus average) are much simpler; a single pass is all you need. And BTW, if you were targeting SSE4.1 instead of just baseline x86-64, the min/max would be even easier with [`pmaxsd`](https://github.com/HJLebbink/asm-dude/wiki/PMAXSB_PMAXSW_PMAXSD_PMAXSQ) and `pminsd` to update vectors of min/max. – Peter Cordes Jan 31 '18 at 22:20
  • `length dd ($ - lst) / 4` would be better than hard-coding 100. I guess you only need it as an input to `idiv`, so using `equ` to make it an assembler constant instead of storing it in memory doesn't actually help. BTW, you should use `DEFAULT REL` at the top of your file so effective addresses like `[length]` will use RIP-relative addressing instead of (longer) absolute. – Peter Cordes Jan 31 '18 at 22:23
  • I got them all working, however I am attempting to now do the sum, count and average for the even numbers in the array and then for the numbers divisible by ten. How can I differentiate to only store the even numbers into eax and then store all the numbers divisible by ten into eax? – Denis Dimitrov Feb 01 '18 at 18:16

0 Answers0