Home > front end >  Increment and decrement same number TASM
Increment and decrement same number TASM

Time:01-16

Already made this code that increments the number that I type, but I'm having trouble with making it decrement the same number that I typed. I thought about moving the number from DL into BL back then DEC it and just show it again but it just gives me the first number I typed, not the DEC'd one.

.model small
.stack 200h
.data
mesaj1 db 13,10, "Introduceti un numar: $"
mesaj2 db 13,10, "Numarul incrementat este: $"
mesaj3 db 13,10, "Numarul decrementat este: $"
.code
main proc
    mov ax, @data
    mov ds, ax
    
    mov dx, offset mesaj1 
    mov ah,09h
    int 21h
    
    mov ah,01h            
    int 21h
    
    sub al,48     
    mov bl,al
    
    inc bl       
    add bl,48
    
    mov dx, offset mesaj2
    mov ah,09h
    int 21h
    
    mov dl,bl         ;
    mov ah,02h
    int 21h
    
    mov bl,al
    dec bl
    
    mov dx, offset mesaj3
    mov ah,09h
    int 21h
    
    mov dl,bl
    mov ah,02h
    int 21h
    
    mov ah, 4ch
    int 21h
    main endp
end main

This is what it gives me1 , I assume that it DECs the INC'd number not the one I typed.

Tried moving what I put into DL back into BL, DEC it, then show it but it doesn't work like that I guess.

CodePudding user response:

The order of the operations matters!

  1. BL is inputted number [0,9]
  2. Display mesaj2
  3. Move BL incremented into DL
  4. Display number
  5. Display mesaj3
  6. Move BL decremented into DL
  7. Display number

Code:

mov  dx, offset mesaj2
mov  ah, 09h
int  21h

mov  dl, bl
add  dl, 48   1
mov  ah, 02h
int  21h

mov  dx, offset mesaj3
mov  ah, 09h
int  21h

mov  dl, bl
add  dl, 48 - 1
mov  ah, 02h
int  21h

An alternative code (4 bytes shorter):

mov  dx, offset mesaj2
mov  ah, 09h
int  21h

lea  dx, [bx   48   1]
mov  ah, 02h
int  21h

mov  dx, offset mesaj3
mov  ah, 09h
int  21h

lea  dx, [bx   48 - 1]
mov  ah, 02h
int  21h

CodePudding user response:

Alright I figured it out, I just put a mov cl,al when I also did mov bl,al in order to save it then after I incremented it I did the same thing but to cl instead of bl and DEC'd it.

  • Related