8 Replies - 3855 Views - Last Post: 12 October 2011 - 04:12 AM

#1 D0kie_D0k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 11

Assembly program that display the sum and difference in different base

Posted 06 October 2011 - 08:42 AM

Good day!
Is This A Good Question/Topic? 0
  • +

Replies To: Assembly program that display the sum and difference in different base

#2 D0kie_D0k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 11

Re: Assembly program that display the sum and difference in different base

Posted 06 October 2011 - 08:54 AM

Can anyone help me do this? My partner and I do find difficulties in converting values from decimal to any base... Could anyone help us please? :(


Write an assembly program that will ask the user to input two binary numbers A and B. The program will then perform addition ( A + B ) and subtraction ( A - B ) operations. Then the program will display the sum and difference in different bases: binary, decimal and octal.


Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9586
  • View blog
  • Posts: 36,323
  • Joined: 12-June 08

Re: Assembly program that display the sum and difference in different base

Posted 06 October 2011 - 08:57 AM

Do not open duplicate topics.

thanks!
Was This Post Helpful? 0
  • +
  • -

#4 D0kie_D0k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 11

Re: Assembly program that display the sum and difference in different base

Posted 06 October 2011 - 09:04 AM

Sorry I got problem with the internet by the time I posted this.. this is the real thing...

Good day!


Can anyone help me do this? My partner and I do find difficulties in converting values from decimal to any base... Could anyone help us please? :(


Write an assembly program that will ask the user to input two binary numbers A and B. The program will then perform addition ( A + B ) and subtraction ( A - B ) operations. Then the program will display the sum and difference in different bases: binary, decimal and octal.


Thanks.

This post has been edited by D0kie_D0k: 06 October 2011 - 09:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

Re: Assembly program that display the sum and difference in different base

Posted 06 October 2011 - 08:11 PM

So, what mnemonic would you use for ADDition and SUBtraction? Displaying in different bases? Here is a hint, ALL numbers are ALREADY binary, so all you have to do is figure out HOW to display the number in bin, dec, and oct (you do know you forgotten hex). Google "base conversion" and you will learn how to convert between different bases. Then,

1. Write some code in your Assembler that does the conversion
2. Write code to display that number (GUI or Non-GUI)
3. Get stuck? Post the code you have written so far and where you are stuck.

4. THEN we will help. We don't spoon feed you here, you have to show some work on your part.
Was This Post Helpful? 1
  • +
  • -

#6 D0kie_D0k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 11

Re: Assembly program that display the sum and difference in different base

Posted 08 October 2011 - 03:53 AM

View PostGunnerInc, on 06 October 2011 - 08:11 PM, said:

So, what mnemonic would you use for ADDition and SUBtraction? Displaying in different bases? Here is a hint, ALL numbers are ALREADY binary, so all you have to do is figure out HOW to display the number in bin, dec, and oct (you do know you forgotten hex). Google "base conversion" and you will learn how to convert between different bases. Then,

1. Write some code in your Assembler that does the conversion
2. Write code to display that number (GUI or Non-GUI)
3. Get stuck? Post the code you have written so far and where you are stuck.

4. THEN we will help. We don't spoon feed you here, you have to show some work on your part.



Okay... Thanks! :)

By the way for now we only got codes for input, still working with the conversion... Thanks for the hint. :)

This post has been edited by D0kie_D0k: 08 October 2011 - 03:57 AM

Was This Post Helpful? 0
  • +
  • -

#7 D0kie_D0k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 11

Re: Assembly program that display the sum and difference in different base

Posted 09 October 2011 - 03:13 AM

this is what we got for now, we got stuck in converting values, we know how to convert it manually but with .asm we got no idea... :(




; input8 bit binary number and print out decimal to screen.
; zeros and ones -> decimal value

ORG 100h

; macro

; this macro prints a char in AL and advances
; the current cursor position:
PUTC    MACRO   char
        PUSH    AX
        MOV     AL, char
        MOV     AH, 0Eh
        INT     10h     
        POP     AX
ENDM

.data
; null terminated input string:
DB "0"
s1 DB "00000000", 0
sum DW 0  ; result.
sum1 DW 0  ; 2nd result 
flag DB 0

;=======================1st input
.code
CALL print
DB "===========PROGRAM EXERCISE 3==========="
DB 0dh, 0ah, "Binary input A: ", 0

input:
MOV DX, 9   ; buffer size (1+ for zero terminator).
LEA DI, s1
CALL GET_STRING

; check that we really got 8 zeros and ones
MOV CX, 8
MOV SI, OFFSET s1
check_s:
        CMP [SI], 0 
        JNE ok0         
        MOV flag, 1     ; terminated.
        JMP convert
    ok0:
        CMP [SI], 'b' 
        JNE ok1         
        MOV flag, 1     ; terminated.
        JMP convert        
    ok1:    
        ; wrong digit? Not 1/0?
        CMP [SI], 31h
        JNA ok2
        JMP error_not_valid     
    ok2:
        INC SI
    LOOP check_s
                      






; start the conversion from string to value in SUM variable.
convert:
MOV BL, 1   ; multiplier.
MOV CX, SI
SUB CX, OFFSET s1
DEC SI

JCXZ stop_program

next_digit:
    MOV AL, [SI]  ; get digit.
    SUB AL, 30h
    MUL BL      ; no change to AX.
    ADD SUM, AX
    SHL BL, 1
    DEC SI          ; go to previous digit.
    LOOP next_digit

; done! converted number is in SUM.

; check if signed
TEST sum, 0000_0000_1000_0000b

print_unsigned:
CALL print
DB 0dh, 0ah, "decimal: ", 0
MOV  AX, SUM
CALL PRINT_NUM_UNS

;========================2nd input
CALL print
DB 0dh, 0ah, "Binary input B: ", 0 
;==================operations function
opr:
call print
DB 0dh, 0ah, "Sum: ", 0
mov ax,(sum1+sum)
call print_num    ; print ax value.

call print
DB 0dh, 0ah, "Difference: ", 0
sub ax,(sum1+sum) 
call print_num    ; print ax value.
JMP  stop_program


;===================error checking
error_not_valid:
CALL print
DB 0dh, 0ah, "error: only zeros and ones are allowed!", 0

stop_program:

; wait for any key....
CALL print
DB 0dh, 0ah, "press any key...", 0
MOV AH, 0
INT 16h
RET


; procedures



; copied from c:\emu8086\emu8086.inc
GET_STRING      PROC    NEAR
PUSH    AX
PUSH    CX
PUSH    DI
PUSH    DX

MOV     CX, 0                   ; char counter.

CMP     DX, 1                   ; buffer too small?
JBE     empty_buffer            ;

DEC     DX                      ; reserve space for last zero.


;============================
; loop to get and processes key presses:

wait_for_key:

MOV     AH, 0                   ; get pressed key.
INT     16h

CMP     AL, 13                  ; 'RETURN' pressed?
JZ      exit


CMP     AL, 8                   ; 'BACKSPACE' pressed?
JNE     add_to_buffer
JCXZ    wait_for_key            ; nothing to remove!
DEC     CX
DEC     DI
PUTC    8                       ; backspace.
PUTC    ' '                     ; clear position.
PUTC    8                       ; backspace again.
JMP     wait_for_key

add_to_buffer:

        CMP     CX, DX          ; buffer is full?
        JAE     wait_for_key    ; if so wait for 'BACKSPACE' or 'RETURN'...

        MOV     [DI], AL
        INC     DI
        INC     CX
        
        ; print the key:
        MOV     AH, 0Eh
        INT     10h

JMP     wait_for_key
;============================

exit:

; terminate by null:
MOV     [DI], 0

empty_buffer:

POP     DX
POP     DI
POP     CX
POP     AX
RET
GET_STRING      ENDP




; copied from c:\emu8086\emu8086.inc
PRINT_NUM       PROC    NEAR
        PUSH    DX
        PUSH    AX

        CMP     AX, 0
        JNZ     not_zero

        PUTC    '0'
        JMP     printed_pn

not_zero:
        ; the check SIGN of AX,
        ; make absolute if it's negative:
        CMP     AX, 0
        JNS     positive
        NEG     AX

        PUTC    '-'

positive:
        CALL    PRINT_NUM_UNS
printed_pn:
        POP     AX
        POP     DX
        RET
ENDP



; copied from c:\emu8086\emu8086.inc
PRINT_NUM_UNS   PROC    NEAR
        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX

        ; flag to prevent printing zeros before number:
        MOV     CX, 1

        ; (result of "/ 10000" is always less or equal to 9).
        MOV     BX, 10000       ; 2710h - divider.

        ; AX is zero?
        CMP     AX, 0
        JZ      print_zero

begin_print:

        ; check divider (if zero go to end_print):
        CMP     BX,0
        JZ      end_print

        ; avoid printing zeros before number:
        CMP     CX, 0
        JE      calc
        ; if AX<BX then result of DIV will be zero:
        CMP     AX, BX
        JB      skip
calc:
        MOV     CX, 0   ; set flag.

        MOV     DX, 0
        DIV     BX      ; AX = DX:AX / BX   (DX=remainder).

        ; print last digit
        ; AH is always ZERO, so it's ignored
        ADD     AL, 30h    ; convert to ASCII code.
        PUTC    AL


        MOV     AX, DX  ; get remainder from last div.

skip:
        ; calculate BX=BX/10
        PUSH    AX
        MOV     DX, 0
        MOV     AX, BX
        DIV     CS:ten  ; AX = DX:AX / 10   (DX=remainder).
        MOV     BX, AX
        POP     AX

        JMP     begin_print
        
print_zero:
        PUTC    '0'
        
end_print:

        POP     DX
        POP     CX
        POP     BX
        POP     AX
        RET
ten             DW      10      ; used as divider.      
ENDP


; print text that follows the caller
print PROC
MOV     CS:temp1, SI  ; store SI register.
POP     SI            ; get return address (IP).
PUSH    AX            ; store AX register.
next_char:      
        MOV     AL, CS:[SI]
        INC     SI            ; next byte.
        CMP     AL, 0
        JZ      printed_ok
        MOV     AH, 0Eh       ; teletype function.
        INT     10h
        JMP     next_char     ; loop.
printed_ok:
POP     AX            ; re-store AX register.
; SI should point to next command after
; the CALL instruction and string definition:
PUSH    SI            ; save new return address into the Stack.
MOV     SI, CS:temp1  ; re-store SI register.
RET   

temp1  DW  ?    ; variable to store original value of SI register.
ENDP

This post has been edited by modi123_1: 09 October 2011 - 11:14 AM
Reason for edit:: please use code tags

Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

Re: Assembly program that display the sum and difference in different base

Posted 09 October 2011 - 08:04 AM

Ok, How would you do the conversion on paper? Convert bin > dec/hex, dec > bin/hex, hex > bin/dec on paper. Now look at how you did that, what mnemonics in Assembly would you use?

There is a button on the reply area called "code" click that, then insert your code in the code block, it will format your code.

This post has been edited by GunnerInc: 09 October 2011 - 08:05 AM

Was This Post Helpful? 0
  • +
  • -

#9 D0kie_D0k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 11

Re: Assembly program that display the sum and difference in different base

Posted 12 October 2011 - 04:12 AM

We already got the correct code... Thanks for the help. :)


; input8 bit binary number and print out decimal to screen.
; zeros and ones -> decimal value

ORG 100h
 
 
print_new_line macro
    mov dl, 13
    mov ah, 2
    int 21h   
    mov dl, 10
    mov ah, 2
    int 21h      
endm

PUTC    MACRO   char
        PUSH    AX
        MOV     AL, char
        MOV     AH, 0Eh
        INT     10h     
        POP     AX
ENDM



.data
; null terminated input string:
    DB "0"
    s1 DB "0000", 0 
    s2 DB "0000", 0 
    presum DB "0000", 0
    sum DW 0  ; result.
    flag DB 0  
    octal db "00", 0
    hexa db 0, 0


.code
    CALL print
    DB 0dh, 0ah, "enter a binary number(please input 4 digits): ", 0



; get string:
    MOV DX, 5   ; buffer size (1+ for zero terminator).
    LEA DI, s1
    CALL GET_STRING
    MOV SI, OFFSET s1
    ; check that we really got 8 zeros and ones
    MOV CX, 4

check:
        CMP [SI], 0 
        JNE ok         
        MOV flag, 1     ; terminated.
    ok:
        CMP [SI], 'b' 
        JNE okena         
        MOV flag, 1     ; terminated.        
    okena:    
        ; wrong digit? Not 1/0?
        CMP [SI], 31h
        JNA okie
        JMP error_not_valid     
    okie:
        INC SI
    LOOP check 

    print_new_line

    CALL print
    DB 0dh, 0ah, "enter another binary number(please input 4 digits): ", 0 

; get string:
    MOV DX, 5   ; buffer size (1+ for zero terminator).
    LEA DI, s2
    CALL GET_STRING 
    MOV SI, OFFSET s2
    MOV CX, 4
check2:
        CMP [SI], 0 
        JNE ok2         
        MOV flag, 1     ; terminated.
    ok2:
        CMP [SI], 'b' 
        JNE okena2         
        MOV flag, 1     ; terminated.        
    okena2:    
        ; wrong digit? Not 1/0?
        CMP [SI], 31h
        JNA okie2
        JMP error_not_valid     
    okie2:
        INC SI
    LOOP check2 
    print_new_line 
 
 
    xor     bx, bx
    
; setup the loop:
    mov     cx, 4 
    mov 	bx, 3  ; point to lest significant digit! 
    mov     ah, 0
        
next_digit:

        ; add digits:
         mov     al, s1[bx]
        add     al, s2[bx] 
        add     al, ah 
        aaa  
        xor     ah, ah
        cmp     al, 3
        jb      two
        mov     ah, 1
        mov     al, 1 
two:    cmp     al, 2
        jb     continue 
        mov     ah, 1
        mov     al, 0    
continue:         
        mov     presum[bx], al
        dec     bx
        xor     al, al
        loop    next_digit
        print_new_line 
        print_new_line     
        mov dx, offset msg1
        mov ah, 9
        int 21h
        print_new_line
        print_new_line 
         CALL print
 DB 0dh, 0ah, "in BINARY is: ", 0
        CALL DISPLAY

convertBINARY:        
    MOV DL, 1   ; multiplier.  
    MOV BX, 3 
    MOV CX, 4

    next_digit1:
        MOV AL,  presum[bx]  ; get digit.
        SUB AL, 30h
        MUL DL 
        ADD SUM, AX
        AAA
        MOV AL,DL
        MOV DL,2
        MUL DL
        MOV DL,AL
        DEC BX
        LOOP next_digit1 
         
   CALL print
    DB 0dh, 0ah, "in DECIMAL is: ", 0
    MOV  AX, SUM 
    CALL OUTPUT 
    print_new_line
    

    
 CALL print
 DB 0dh, 0ah, "in OCTAL is: ", 0
     MOV BX,1
     MOV CX,2 
     MOV AX, SUM   
convertOCTAL:   
    MOV ah, 0
    MOV DL, 8
    DIV DL  
    MOV OCTAL[BX],AH 
    SUB AL, 8
    AAS
    DEC BX
    LOOP convertOCTAL 
    CALL DISPLAYoctal 
    
convertHEXA: 
    CALL print
    DB 0dh, 0ah, "in HEXADECIMAL is: ", 0
    MOV AX, SUM
    CALL OUTPUT
    CALL LETTERS
     
    JMP  stop_program
error_not_valid:
    CALL print
    DB 0dh, 0ah, "only zeros and ones are allowed!", 0

stop_program:
; wait for any key....
    CALL print
    DB 0dh, 0ah, "press any key...", 0
    MOV AH, 0
    INT 16h
    RET





;INPUT
GET_STRING      PROC    NEAR
        PUSH    AX
        PUSH    CX
        PUSH    DI
        PUSH    DX
        
        MOV     CX, 0                   ; char counter.
        
        CMP     DX, 1                   ; buffer too small?
        JBE     empty_buffer            ;
        
        DEC     DX                      ; reserve space for last zero.
        
    
;============================
; loop to get and processes key presses:

wait_for_key:
        
        MOV     AH, 0                   ; get pressed key.
        INT     16h
        
        CMP     AL, 13                  ; 'RETURN' pressed?
        JZ      exit
        
        
        CMP     AL, 4                   ; 'BACKSPACE' pressed?
        JNE     add_to_buffer
        JCXZ    wait_for_key            ; nothing to remove!
        DEC     CX
        DEC     DI
        PUTC    4                       ; backspace.
        PUTC    ' '                     ; clear position.
        PUTC    4                       ; backspace again.
        JMP     wait_for_key

add_to_buffer:

        CMP     CX, DX          ; buffer is full?
        JAE     wait_for_key    ; if so wait for 'BACKSPACE' or 'RETURN'...

        MOV     [DI], AL
        INC     DI
        INC     CX
        
        ; print the key:
        MOV     AH, 0Eh
        INT     10h

        JMP     wait_for_key
;============================

exit:

; terminate by null:
         MOV     [DI], 0

empty_buffer:

        POP     DX
        POP     DI
        POP     CX
        POP     AX
        RET                   
GET_STRING      ENDP   



print PROC
    MOV     CS:temp1, SI  ; store SI register.
    POP     SI            ; get return address (IP).
    PUSH    AX            ; store AX register.
next_char:      
        MOV     AL, CS:[SI]
        INC     SI            ; next byte.
        CMP     AL, 0
        JZ      printed_ok
        MOV     AH, 0Eh       ; teletype function.
        INT     10h
        JMP     next_char     ; loop.
printed_ok:
        POP     AX            ; re-store AX register.
        ; SI should point to next command after
        ; the CALL instruction and string definition:
        PUSH    SI            ; save new return address into the Stack.
        MOV     SI, CS:temp1  ; re-store SI register.
        RET
        temp1  DW  ?    ; variable to store original value of SI register.
ENDP   



DISPLAY PROC NEAR
; print out the result:
    mov     cx, 4 
; start printing from most significant digit:
    mov     bx, 0         

print_d:
        mov     al, presum[bx]
        ; convert to ascii char:
        or      al, 30h

        mov     ah, 0eh
        int     10h
        
        inc     bx
        
        loop    print_d  
RET
DISPLAY ENDP

        

DISPLAYoctal PROC NEAR 
    mov     CX, 2
    mov     bx, 0 
printo:
        mov     al, OCTAL[bx]
        ; convert to ascii char:
        or      al, 30h

        mov     ah, 0eh
        int     10h
        
        inc     bx
        
        loop    printo
RET
DISPLAYoctal ENDP 

 msg1 db "THE SUM  $"  
 or1  db " or $"  
 
OUTPUT PROC NEAR
    MOV AX, SUM 
    xor     ah, ah
    aaa
    mov     dx, ax
    ; print first digit:
    mov     ah, 0eh
    ; convert to ascii:
    or      dh, 30h
    mov     al, dh
    int     10h 

    ; print second digit:
    ; convert to ascii:
    or      dl, 30h
    mov     al, dl
    int     10h   
    ret
OUTPUT ENDP


LETTERS PROC NEAR 
     MOV AX, SUM 
    xor     ah, ah
    aaa 
    CMP AH, 1
    JNE  end
    mov dx, offset or1
    mov ah, 9
    int 21h
    MOV AX, SUM
     xor     ah, ah
    aaa
   
TEN:
     CMP AL,0 
    JNE ELEVEN
    mov     al, 'A'
    mov     ah, 0eh
    int     10h
ELEVEN: CMP AL, 1
        JNE TWELVE
     mov     al, 'B'
    mov     ah, 0eh
    int     10h
TWELVE: CMP AL, 2
        JNE THIRTEEN
    mov     al, 'C'
    mov     ah, 0eh
    int     10h        
    
THIRTEEN:CMP AL, 3
         JNE FOURTEEN
    mov     al, 'D'
    mov     ah, 0eh
    int     10h 
    
FOURTEEN:CMP AL, 4 
         JNE FIFTEEN
    mov     al, 'E'
    mov     ah, 0eh
    int     10h
FIFTEEN:   CMP AL, 4 
         JNE end
    mov     AX, 'F'
    mov     ah, 0eh
    int     10h 
end:    ret
LETTERS ENDP   

This post has been edited by D0kie_D0k: 12 October 2011 - 04:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1