in reply to Re^3: How to assign a variable its width/vector
in thread How to assign a variable its width/vector

I am sorry about that. I have pasted the code i am trying out but i know it has a major flaw in it. Especially when i decalre $length_buffer, what i really want to say is that my $length_buffer is 32bytes wide i.e it can hold 256 bits. Anytime i read out an instrution address such as

0x405d75 0x4035f7 0x4035f8 0x4035fb

i compute its length and diplay it in bytes so i could use it for comparing the length of the address and the buffer in bytes. the source from where i picked up using the pragma Use bytes is below. (http://perlmeme.org/howtos/using_perl/length.html)

#!user/bin/perl #use strict; #use warnings; use bytes; $length_buffer= length(0x100) ; + #buffer length print "$length_buffer, \n"; $base=length(0x00); + #base address print "$base, \n"; $base_end= ($base+ $length_buffer-1); + #base_end address, here it means it goes from 0 to 255 which is 25 +6 elements in the buffer print " The end of the buffer=$base_end, \n"; + #just to check if the buffer end prints correctly, in this case it +s 255 #$temp= 0xFFFFFFE0; $count_hit=0; + #counter to calculate the number of hits $count_miss=0; + #counter to calculate number of misses ################## Input and output Files############################# +####################################################### open(FILE1," < output_log.txt") or die "can't read the file"; + #trace file input open(FILE2, "> o_hit_miss.txt") or die "can't write into the file"; + #output file containing the hit and miss informaiton ###################################################################### +######################################################### while(<FILE1>){ $inst_addr= $_; # + read the lines and store in in inst_addr #print " $inst_addr \n"; +# to check if the inst_addr is being read out correctly from the give +n file ###########################################calcultes the length of the + address and prints the same in bytes#################### if($inst_addr) { #use bytes; # Pr +agma to display the string in bytes and not in characters #$len_ofinstr=length( $inst_addr); +#calculates the length of string in bytes and assigns it to $len_inst +r #print " Length in bytes: ", $len_ofinstr, "\n"; +#prints the length in bytes for my understanding if($len_ofinstr >= $base && $len_ofinstr <=$base_end) { #print" $hit, \n"; #print FILE2 $hit. "\n"; #print " The inst fits in the buffer->hit, \n"; $len_ofinstr+=$len_ofinstr; #print "Total number of bytes in the buffer=", $len_ofinstr, " +\n"; $count_hit=$count_hit+1; #print " Number of hits in the buffer= $count_hit", "\n"; print FILE2 "number of hits in buffer". $count_hit. "\n"; } } else{ if($len_inst == $length_buffer) { print " it can not hold anymore, \n"; $base= $instr_addr & 0xffffffe0; } } }

Replies are listed 'Best First'.
Re^5: How to assign a variable its width/vector
by GrandFather (Saint) on Apr 15, 2014 at 07:02 UTC

    First off, uncomment strictures and fix the errors reported. At the very least, as your code stands, $len_ofinstr and $len_inst are used before they are assigned a value which is likely to be wrong.

    What do you expect length to do?

    What do you expect 'use bytes' to do? (The module's documentation says "use of this module for anything other than debugging purposes is strongly discouraged".)

    Writing a test script of the form:

    #!user/bin/perl use warnings; use strict; my $inFile = <<FILE; FILE ... open my $fIn, '<', \$inFile; while (defined (my $line = <$fIn>)) { ... }

    and telling use what you expect as output would go a long way to making your issue clear to us. The open ... \$inFile; bit opens a string as a file which is a nice way of including sample data in your script while retaining the structure of file I/O of the full script.

    Perl is the programming world's equivalent of English

      Basically i am trying to read a hexadecimal string such as the below mentioned

      0x401d50 0x360aa7c030 0x405dc2 0x405dc9 0x405dd0 0x405dd5 0x405dd8 0x401bb0

      compute the length of the same in bytes and compare to see if its lesser than 32 bytes(which is the size of a register that is going to hold all these addresses, which i am referring to as buffer) and if the comparison satisfies then i keep loading the instrutions until the register is full i.e unti lit reaches 32 bytes. Once it does i call it a miss .

      #! user/bin/perl #use strict; #use warnings; $length_buffer= 0x100; $base= 0; $base_end= ($base+ $length_buffer-1); open(FILE1," < output_log.txt") or die "can't read the file"; + while(<FILE1>) { $inst_addr= $_; if($inst_addr) { use bytes; + $len_ofinstr=length( $inst_addr); + print " Length in bytes: ", $len_ofinstr, "\n"; + if($len_ofinstr >= $base && $len_ofinstr <=$base_end) { print FILE2 $hit. "\n"; print " The inst fits in the buffer->hit, \n"; $len_ofinstr+=$len_ofinstr; print "Total number of bytes in the buffer=", $len_ofinstr, "\ +n"; $count_hit=$count_hit+1; print " Number of hits in the buffer= $count_hit", "\n"; print FILE2 "number of hits in buffer". $count_hit. "\n"; } } else{ if($len_inst == $length_buffer) { print " it can not hold anymore, \n"; $base= $instr_addr & 0xffffffe0; } } }

      The snippet of the code is pasted above, the problem i am facing with the above pasted code is that, when $length_buffer= 0x100 (equivalent of the number 256 but not euvalent of 256 bits or 32 bytes) so the comparison is not really making any sense. Could anyone suggest how to fix this issue? thanks

        You seem to need the size (or length) neither in chars nor in bytes, but in bits.

        I think you can take the dec2bin from How do I convert between decimal and binary?

        Then, length dec2bin 0x100 (or length(dec2bin(0x100))) gives you a result of 9.

        Alternatively, to see if an address fits in 32 bits, you could compare it to 0xffffffff, which is the biggest value to fit.