0

I have a task to take an existing assembly program that prints signed number (word sized), and I need to change it so it will print unsigned numbers (word sized)... Please help me understand the difference and how should I accomplish this.

That is the program that prints signed number:

.model  small
.stack   100h
.data
num  dw  -32768
numS db  6 dup(' '),'$'

.code
    mov ax, @data
    mov ds, ax

    mov ax, num
    mov bx, 10

    mov si, offset numS+5

next:   
        cwd
    idiv bx
    cmp dx, 0
    jge cont
    neg dx
cont:
    add dl, 48
    mov [si], dl
    dec si
        cmp ax, 0
    jz sof
    jmp next
sof:    
    cmp num, 0
    jge soff
    mov byte ptr[si],   '-'
soff:
    mov ah, 9
    mov dx, si
    int 21h
    .exit
end

Thanks!

Joe
  • 2,543
  • 3
  • 25
  • 49
  • The max signed 8-bit number is 01111111 = 127, while the maximum unsigned 8-bit number is 11111111 = 255. Signed has a sign bit, unsigned uses all bits for the actual number. – Hot Licks May 31 '14 at 23:09
  • Sort of a duplicate of [Displaying numbers with DOS](https://stackoverflow.com/q/45904075) - this appears to be really weird, using `idiv` to figure out if the input is negative instead of `test ax,ax` / `jge` once ahead of the loop. Normally you just print abs(x) with a `-` or not. – Peter Cordes Feb 15 '22 at 18:29

1 Answers1

1

Since your word size is 16 bits, the range of signed numbers will be from -32768 to 32767, where the range for unsigned numbers goes from 0 to 65535. And while you're declaring num as -32768, the computer represents this in hex as 0x8000, which if it's performing signed operations will be -32768, but if it is performing unsigned operations will be +32768.

If you use a different negative number, say -1, this is 0xFFFF in hex. If you perform an unsigned operation on it, then it will be interpreted as 65535.

Note that the idiv instruction does signed division, whereas the div instruction does unsigned.

mattb
  • 26
  • 2
  • Also, if you are printing just unsigned characters, the line "mov byte ptr[si], '-'" should go away (and the two lines above it) since they are adding the negative sign to the string to print. – mattb Jun 01 '14 at 00:01