Results 1 to 3 of 3

Thread: Absolute value

  1. #1
    ChimpFace9000
    Guest

    Post Absolute value

    How do i get the absolute value of a number? I thought maybe there was an instruction that did it, but i couldnt find one.

  2. #2
    Knight_Vision
    Guest
    Hehe... I was wondering that myself.. LOL

    I'm still learning. There is Soooo much in assembly to learn.. uhhgg...

  3. #3
    jim mcnamara
    Guest
    I haven't coded this stuff for long time, and I don't have MASM anymore. Please just take this as an explanation. I'm not sure about parts of this. But, I'm very sure the part from START thru $RESTORE is correct.

    The algorithm for abs is
    compare value to zero
    jump to return if true
    neg the value
    return:
    put value in eax

    Here's a *** version for returning the abs of an integer -
    but you'll get the idea.

    Code:
    PUBLIC	?abs					; abs
    _TEXT	SEGMENT
    _a = 8                                   
    ?absi PROC NEAR					; abs
    ; abs function for integers only
    
    	push	ebp                              ; preserve it
    	mov	ebp, esp                         ; get passed value location (frame)
    	sub	esp, 64			        
    	push	ebx
    	push	esi
    	push	edi
    	lea	edi, DWORD PTR [ebp-64]
    	mov	ecx, 16				
    	mov	eax, -858993460			
    	rep stosd
    $START:
           mov   eax, DWORD PTR _a[ebp]      ; place value in eax
    	cmp	eax, 0                              ; is it > = 0
    	jl	SHORT $MINUS                        ; no
    	jmp	SHORT $RESTORE                      ; yes, return
    $MINUS:
    	neg	eax                                 ; value * -1     
    $RESTORE:
    	pop	edi
    	pop	esi
    	pop	ebx
    	mov	esp, ebp
    	pop	ebp
    	ret	0
    ?absi ENDP					; abs1
    _TEXT	ENDS
    END

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width