[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Normalize two float numbers and sum them?

Hello I have this

45.32
0        exponent       mantisa
sign    10000100      1 .01101010100011110101110  
0.2111
0        01111100      1 .10110000001000001100010

As far as I know If I want to sum I must MATCH the exponents.

Can you please tell me the code to make the exponents mach, after it I think I can sum them easily

 SumarFloat proc var1:REAL4, var2:REAL4
    LOCAL retval:REAL4
    PUBLIC SumarFloat
   
 
    ret
SumarFloat endp
0
djhex
Asked:
djhex
  • 4
  • 2
1 Solution
 
grg99Commented:
Take the difference in the exponents, then shift one of the mantissas by that many bits.
Something roughly like:

              mov    eax,45.32
               mov   ebx,0.2111

              mov     edx,eax     ; copy the number
              shr       edx,24     ; or however many bits shift needed
              and      edx,0xFFF ;  or however many bits in exponent

              mov     esi,eax     ; copy the number
              shr       esi,24     ; or however many bits shift needed
              and      esi,0xFFF ;  or however many bits in exponent

              sub     edx,esi    ; subtract second number's exponent from the first

             mov     ecx,edx           ; save shift count for later

              and       eax,0x00FFFFFF    ; or however many bits in the mantissa
              or         eax, 0x0100000     ; add assumed normalized bit

              and       ebx,0x00FFFFFF    ; or however many bits in the mantissa
              or         ebx, 0x0100000     ; add assumed normalized bit

               asr      ebx,cl                     ;  shift second number into position

                add      eax,ebx      ;  do the addition


0
 
libin_vCommented:
let me explain the c code in assembly.

I am not familiar with assembly, though i learnt it 2 years ago. There are surely lots of bugs, dummy statements which may not exist but does mean something in assembly. I have done a lot of repetion of code, because i do not know structured assembly programming. Please bare with me. I hope u will understand this code. I hope u r luck.

let var1:real
let var2:real
let tmp:dword
let m1:dword      ;m => mantissa
let m2:dword
let e1:byte      ;e => exp
let e2:byte
let s1:byte      ;s =>sign
let s2:byte
lea dx, var1
mov tmp, [dx]      ;load the value at dx as dword
shr tmp, 23d      ;shift 23 bits to remove mantissa
and tmp, 000000ff      ;remove sign bit
            ;thus remaining exponent of var1
mov e1, tmp      ;load to e1

            ;now, do same with var 2
lea dx, var2
mov tmp, [dx]      ;load the value at dx as dword
shr tmp, 23d      ;shift 23 bits to remove mantissa
and tmp, 000000ff      ;remove sign bit
            ;thus remaining exponent of var2
mov e2, tmp      ;load to e1

cmp e1, e2      ;compare exponents
jb var1less      ;shift var1 if below
ja var2less      ;shift var2 if above
je doadd            ;since same do addition

var1less :            ;shifting var1
      lea dx, var1
      mov tmp, [dx]      ;load the value at dx as dword
      and tmp, 80000000h
                  ;test for sign bit
      cmp tmp, 0
      je nochangem1      ;dont cahnge mantissa
      mov bx, 1      ;set flag for no change
      jump continuem1
nochangem1:
      mov bx, 0
continuem1:
      lea dx, var1
      mov tmp, [dx]      ;load the value at dx as dword
      and tmp, 007fffffh      ;clear all but mantissa (23 bits)
                  ;7fffff is 23 bits
      mov m1, tmp      ;load to m1
      mov ax, e2      ;do e2-e1
      sub ax, e1      
      mox cx, ax      ;prepare for shifting
      again :
            shr m1, 1            ;shift by the difference of exponents
            cmp bx, 1
            jne cond            ;do not do any change if no sign
            or m1, 00400000h      ;shifting with sign bit
      cond:      loop again      
      
                  ;changing exp
      mov e1, e2
                  ;now changing var1
      mov [dx], 0      ;clear
      or [dx], m1      ;load mantissa
      shl e1, 23      ;get back exp
      or [dx], e1      ;load exp
      cmp bx, 1      ;load sign bit
      jne doadd
      or [dx], 80000000h
      jump doadd

      ;simillarly shift var2
var1less :            ;shifting var1
      lea dx, var2
      mov tmp, [dx]      ;load the value at dx as dword
      and tmp, 80000000h
                  ;test for sign bit
      cmp tmp, 0
      je nochangem2      ;dont cahnge mantissa
      mov bx, 1      ;set flag for no change
      jump continuem2
nochangem2:
      mov bx, 0
continuem2:
      lea dx, var1
      mov tmp, [dx]      ;load the value at dx as dword
      and tmp, 007fffffh      ;clear all but mantissa (23 bits)
                  ;7fffff is 23 bits
      mov m2, tmp      ;load to m1
      mov ax, e1      ;do e1-e2
      sub ax, e2
      mox cx, ax      ;prepare for shifting
      again2 :
            shr m2, 1            ;shift by the difference of exponents
            cmp bx, 1
            jne cond2            ;do not do any change if no sign
            or m2, 00400000h      ;shifting with sign bit
      cond2:loop again2      
      
                  ;changing exp
      mov e2, e1
                  ;now changing var1
      mov [dx], 0      ;clear
      or [dx], m2      ;load mantissa
      shl e2, 23      ;get back exp
      or [dx], e2      ;load exp
      cmp bx, 1      ;load sign bit
      jne doadd
      or [dx], 80000000h
      jump doadd

doadd:
      ;do addition here
0
 
djhexAuthor Commented:
Please explain me the code step by step as If I was a kid. and you got the points.
Does this sum any kind of numbers(negative,  with negative exponents and so?




Lets try to adapt the code to this procedure.

Why codes are so different in the number of lines?

 SumarFloat proc var1:REAL4, var2:REAL4
    LOCAL retval:REAL4
    PUBLIC SumarFloat
   
   
   
SumarFloat endp
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
djhexAuthor Commented:
Grg99 its asr an instruction?
0
 
djhexAuthor Commented:
Please help me to correct this

SumarFloat proc var1:REAL4, var2:REAL4
    LOCAL retval:REAL4
    PUBLIC SumarFloat
   
       mov    eax, var1
       mov   ebx,var2

       mov     edx,eax     ; copy the number
       shr       edx,24     ; or however many bits shift needed
       and      edx,0xFFF ;  or however many bits in exponent
       mov     esi,eax     ; copy the number
       shr       esi,24     ; or however many bits shift needed
       and      esi,0xFFF ;  or however many bits in exponent
       sub     edx,esi    ; subtract second number's exponent from the first
         mov     ecx,edx           ; save shift count for later
       and       eax,0x00FFFFFF    ; or however many bits in the mantissa
       or         eax, 0x0100000     ; add assumed normalized bit
         and       ebx,0x00FFFFFF    ; or however many bits in the mantissa
       or         ebx, 0x0100000     ; add assumed normalized bit
       asr      ebx,cl                     ;  shift second number into position
       add      eax,ebx      ;  do the addition
   
SumarFloat endp


end;


C:\Masm32\Bin\ML.EXE /c /coff /Cp /nologo /I"C:\Masm32\Include" "pentium.asm"
 Assembling: pentium.asm
pentium.asm(39) : error A2008: syntax error : ebx    and      edx,0xFFF
pentium.asm(29) : error A2206: missing operator in expression   and      esi,0xFFF
pentium.asm(32) : error A2206: missing operator in expression and       eax,0x00FFFFFF    
pentium.asm(35) : error A2206: missing operator in expression  or         eax, 0x0100000    
pentium.asm(36) : error A2206: missing operator in expression  and       ebx,0x00FFFFFF    
pentium.asm(37) : error A2206: missing operator in expression  or         ebx, 0x0100000    
pentium.asm(38) : error A2206: missing operator in expression   asr      ebx,cl                    position

Make error(s) occured.
Total compile time 296 ms


0
 
djhexAuthor Commented:
libin_v


Please help me adapt it

to my snippet
 SumarFloat proc var1:REAL4, var2:REAL4
    LOCAL retval:REAL4
    PUBLIC SumarFloat
   
   
SumarFloat endp
0
 
grg99Commented:
oops, the format for hex constants in MASM isnt like I wrote,  you have to have a leading zero and an "h" suffix.

Also note that the shifts and masks are APPROXIMATE, we have to leave something for you to do.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now