Author Topic: unsigned integers  (Read 11054 times)

Offline travman89

  • Jr. Member
  • *
  • Posts: 4
unsigned integers
« on: March 08, 2010, 05:01:22 AM »
I have an assignment in which i need to use unsigned integers.  The program works properly, but only works with numbers up to 2^31 -1, and i need it to work with numbers up to 2^32 -1.  Is there a way to declare unsigned integers, or is there a way to code so that the registers handle the numbers as unsigned integers.  The way i declared my 2 variables is like this.




resd input 1                               ; one double word
resd array 1000                          ; one array of double words of size 1000

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: unsigned integers
« Reply #1 on: March 08, 2010, 09:14:34 AM »
Do I recall correctly that you're using Dr. Carter's routines? If so, they won't handle unsigned integers, as they stand. If you're ready, take a deep breath and delve into his "asm_io.asm". First thing in "section .data" is "int_format", given as "%i". Both "read_int" and "print_int" use this. To do unsigned integers, you'd want "%u" (as I recall) - it's a C thing. The "easy" fix would be to just change it to "%u". The "right" (I guess) way to fix it would be to add a "uint_format" format string, and a "read_uint" and "print_uint" routine (copied mostly from Dr. Carter's routines). Either way, don't forget to re-assemble "asm_io.asm", which you probably haven't done since you installed the thing. Then link your program against the new asm_io.o[bj], and you'll have the new behavior.

But that may not be the "point" of your assignment. If you're expected to write code to handle unsigned integers "on your own", doing the above probably won't get you a good grade. Or maybe that's the way you're "supposed" to do it. I dunno.

Anyway, the way you declare your variables is the same for signed and unsigned, in asm ("label resd number", not "resd label number" as you show, BTW!). A C compiler needs to know whether it's signed or unsigned, since it has to decide which actual instructions to use. In asm, *you* are the one writing the instructions, and you have to choose appropriate ones. Fortunately, in most cases, there isn't any difference between signed and unsigned. There are exceptions. For example...

Code: [Select]
mov eax, 1
mov ebx, 0FFFFFFFFh
cmp eax, ebx
; jb someplace
; jl someplace

Which do we want? If eax and ebx are unsigned integers, ebx is a big number - bigger than 1. If they're signed integers, ebx is -1 - smaller than 1. Note that the bit pattern in ebx (all of 'em) is the same in both cases, the difference is in how we wish to interpret the bits. If signed, anything "bigger" than 80000000h (most significant bit set) represents a number less than zero.

If you need more help with which instructions differ between signed and unsigned, RTFM or show us what you've got and where it's going wrong. (or both :)

Best,
Frank


Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: unsigned integers
« Reply #2 on: March 08, 2010, 02:50:11 PM »
Also, read up on two's complement arithmetic and you'll have an idea of how the processor represents/expects signed numbers in registers.

Offline travman89

  • Jr. Member
  • *
  • Posts: 4
Re: unsigned integers
« Reply #3 on: April 07, 2010, 07:17:29 AM »
Thank you both for your replies. I was able to get around my problem by "cheating" a little bit.  I faked my unsigned input by converting it to a "string" and then spitting it back out. Since all the numbers i had to print after the 32bit numbers were at most half of the largest number they were able to be printed by Dr. Carters print_int.  Of course after my project was due i was given a print_unsigned function which i now link with all of my projects.  Oh how much simpler life is when you can satisfy your professors undying hatred of signed integers with the call of print_unsigned.