0

I am trying to write assembly procedure which receives a parameter in register ebx, and returns the absolute value of the parameter in register eax, then calling that procedure in a loop to visit every elements of a given array and to get their absolute values. When I run the code, I receive the below segment errors from assembler.

 Assembling: abs.asm
abs.asm(3) : error A2034: must be in segment block : ABS
abs.asm(4) : error A2034: must be in segment block : $$$00001
abs.asm(4) : error A2034: must be in segment block
abs.asm(5) : error A2034: must be in segment block : $$$00001
abs.asm(5) : error A2034: must be in segment block
abs.asm(6) : error A2034: must be in segment block : $$$00001
abs.asm(6) : error A2034: must be in segment block
abs.asm(7) : error A2034: must be in segment block : $$$00001
abs.asm(7) : error A2034: must be in segment block
abs.asm(8) : error A2034: must be in segment block
abs.asm(9) : error A2034: must be in segment block : $$$00001
abs.asm(9) : error A2034: must be in segment block
abs.asm(10) : error A2034: must be in segment block
abs.asm(11) : fatal error A1010: unmatched block nesting : ABS
INCLUDE Irvine32.inc

ABS PROC
cmp ebx,0       ;compare ebx to zero
jle L1          ;Jump if less then or equal to zero
mov eax, ebx    ;copy ebx to eax, means eax is positive
Jmp L2          ;jumps to label return
L1:neg ebx      ;negates ebx    
mov eax,ebx     ;copy neagted ebx to eax        
L2:ret
ABS ENDP

.DATA
arrayW SDWORD 3, -2, 5, 7, 2, 9, -11, 32, -19, 18, 17, 15, -5, 2, 3, 1, -21, 27,-29, 20

.CODE
main PROC

    mov esi, OFFSET arrayW      ;address of arary
    mov ecx, LENGTHOF arrayW    ;loop counter

top:
    mov ebx,[esi]               ; move first element of arrayw to ebx
    call ABS                    ; calls procedure abs and gets the eax value
    call Writeint               ; displays eax  
    add esi, TYPE arrayW        ;points next element in array
    loop top                    ;loop goes to the top
    call clrscr

    exit
main ENDP

END main
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
mwater07
  • 131
  • 4
  • 14
  • 4
    `ABS` is a procedure, which means it is code. Therefore, it should go under the `.CODE` section. (Also, for what it's worth, there are several other ways to write `ABS` that would be *substantially* more efficient. But I guess this is just a learning exercise about branching.) – Cody Gray - on strike Jun 27 '17 at 15:11
  • Yes, you are right. it is just and branching exercise. It is working now. Thank you – mwater07 Jun 27 '17 at 15:16
  • You didn't get errors when you did run the code, but when you tried to compile/assemble it, those are compile-time errors. The runtime errors in assembly look a bit different... basically any random effect is possible, or at least was, modern operating systems are surprisingly sturdy against random machine code execution, but with early computers any mistake in assembly code usually ended by restart of whole machine (losing any progress you didn't save before on some external storage device). – Ped7g Jun 27 '17 at 21:24

1 Answers1

4

I needed to move the ABS procedure under the .CODE section:

INCLUDE Irvine32.inc

.DATA

arrayW SDWORD 3, -2, 5, 7, 2, 9, -11, 32, -19, 18, 17, 15, -5, 2, 3, 1, -21, 27,-29, 20


.CODE

ABS PROC
    cmp ebx,0       ;compare ebx to zero
    jle L1          ;Jump if less then or equal to zero
    mov eax, ebx    ;copy ebx to eax, means eax is positive
    jmp L2          ;jumps to label return
    L1:neg ebx      ;negates ebx    
    mov eax,ebx     ;copy neagted ebx to eax        
    L2:ret
ABS ENDP

main PROC

    mov esi, OFFSET arrayW      ;address of arary
    mov ecx, LENGTHOF arrayW    ;loop counter

top:
    mov ebx,[esi]               ;move first element of arrayw to ebx
    call ABS                    ;calls procedure abs and gets the eax value
    call Writeint               ;displays eax
call Crlf   
    add esi, TYPE arrayW        ;points next element in array
    loop top                    ;loop goes to the top

    exit
main ENDP

END main
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
mwater07
  • 131
  • 4
  • 14