Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

inverting digits

dilate02dilate02 Member Posts: 12
does anyone know how i can create an inverted integer from one saved in ax? For example, how can i create the number 4995 from the number 5994? Thank you!

Comments

  • bigmike500bigmike500 Member Posts: 24
    : does anyone know how i can create an inverted integer from one saved in ax? For example, how can i create the number 4995 from the number 5994? Thank you!
    :

    i am a newbie to assembly so there probably is a better way to do this, and if anyone knows it i would also like to know. the only way that i can think of to do this is to convert the binary number in ax into BCD (binary-coded-decimal) format, then perform a rotate left by 4 on al then the same again on ah, then after that you would need to do a rotate by 8 on ax

    e.g ax=4995 (bcd)
    al=95 (bcd)
    rol al,4
    al=59 (bcd)
    ah=49 (bcd)
    rol ah,4
    ah=94 (bcd)

    ax=9459 (bcd)
    rol ax,8
    ax=5994 (bcd)

    i'm afraid that i can't provide any code for this problem as i am not that good yet, and some of the ideas maybe wrong, like i am not sure if you can perform a rotate on a 8 bit register so you might have to use bx or something for the second pair. also i don't actually know how to convert from decimal to bcd but i am sure that there is someone on this board that knows how to, this is by no means a complete answer but i hope that is helps in some way!
    Mike
  • BitdogBitdog Member Posts: 528
    : does anyone know how i can create an inverted integer from one saved in ax? For example, how can i create the number 4995 from the number 5994? Thank you!
    :
    Ok, if AX holds the decimal value, 1234,
    you want AX to hold the decimal value 4321. (right?)
    If it was a hex value you're talking about,
    you could XCHG AH,AL
    then rotate the bits of each by 4
    but a decimal value is a different story
    (& your 5994 wasn't written as 5994h ).

    I would invert a decimal number the dum way,
    convert the AX value to an decimal ASCII string in memory,
    reverse that string,
    then get the value of the string.
    I would do it that way cause I made PROC's that I can just call
    to do the work for me.

    CALL WMEMDWB ; Write AX to MEMory as a Decimal ASCII #
    ;write some reverse data code here, get & push, pop & write
    CALL GINNUM ; Get INput NUMber from memory into AX reg
    STOSW ; write AX to ES:DI in memory somewhere

    You can have them if you want? at:
    http://bitdog.home.att.net/files/nasmenv.zip
    (A free working Nasm environment with includes, macros, examples, etc)


    I hope that helps somehow.
    If it's for class, you arn't going to look like a genus though.

  • bigmike500bigmike500 Member Posts: 24
    [b][red]This message was edited by bigmike500 at 2003-2-17 4:21:3[/red][/b][hr]
    here is the source code for tasm that demonstrates digit reversing assuming that the input is bcd.
    Mike

    ;this source code shows the rotate function to mess around with values in a register
    ;complied with tasm 5 and tlink
    ;mike 17/02/03


    .model small
    .stack
    .data

    number_input db "insert the 4 digit number that you want to convert: ","$"
    number_out db 10,13,"the inputted number reversed is: ","$"
    continue db 10,13,"press any key to continue","$"

    inputted_num db 5,?,5 dup(?)
    outputted_num db 5,?,5 dup(?),10,13,"$"

    .code
    main proc
    mov ax,@data ;initialises the data segment register
    mov ds,ax

    mov dx, offset [number_input] ;general prompt stuff
    mov ah,09h
    int 21H

    mov ah,0Ah
    mov dx, offset [inputted_num] ;general input stuff
    int 21h

    xor dx,dx ;clean the register
    xor ax,ax ;clean the register
    mov cx,4 ;load count register with number of digits
    mov bx,2 ;gives a offset adjustment to input string
    get_input: ;setting up a bcd number
    mov dl,[inputted_num+bx] ;mov the sperate input digits to a register
    sub dx,30h ;covert from ascii
    add ax,dx ;adds the seperate parts to ax
    dec cx ;decrement counter register
    cmp cx,0 ;if count is zero get out of the loop
    jz convert ;to the convert tag
    inc bx ;moves address to next character in the string
    shl ax,4 ;moves inputted value to give room for the next value
    jmp get_input ;back to loop

    convert: ;ax should equal inputed number in bcd
    rol al,4 ;changing stuff around
    rol ah,4
    rol ax,8

    ;ax now contains the reverse inputted number
    ;so need to put the value of ax to a string to be outputted

    mov bx,2 ;gives a offset adjustment to input string
    mov ch,4 ;counter of the number of digits
    mov cl,12 ;a shift counter
    get_output:
    push ax ;store the reversed number
    shr ax,cl ;shift to the right to get the wanted digit in al
    and al,15 ;clear the higher nibble
    add al,30h ;convert the value to ascii to be output in string
    mov [outputted_num+bx],al ;save to the string
    dec ch ;decrement the digit counter
    cmp ch,0
    jz display_out ;if zero exit the loop
    sub cl,4 ;need to shift 4 bits at a time
    inc bx ;move to next space in string
    pop ax ;reihstore reversed digit
    jmp get_output ;continue loop

    display_out:

    mov dx, offset [number_out] ;general output stuff
    mov ah,09h
    int 21H

    mov dx, offset [outputted_num+2] ;output the reversed digit string
    mov ah,09h
    int 21H

    exit:
    mov dx, offset [continue] ;general output stuff
    mov ah,09h
    int 21H

    xor ax,ax ;press any key to continue
    int 16h

    mov ax,4c00h ;returns control back to dos
    int 21h

    main endp
    end main








Sign In or Register to comment.