x86 - Find and display greatest common divisor from 2 input numbers in nasm -
good day,
i have been trying write program following:
- accept 2 ascii numbers user (i haven't bothered trying check values yet)
- convert ascii numbers decimal values.
- find greatest common divisor 2 numbers
- display result
i feel though i've done steps 1-3, although i'm not entirely sure. here code:
bits 16 org 0x100; jmp main ; number1_str: db 3,0,0,0,0,'$' number2_str: db 3,0,0,0,0,'$' num1_hex: dw 2 num2_hex: dw 2 prompt1: db 'please enter first number (from 1 50): ','$' prompt2: db 'please enter second number (from 1 50): ','$' cr_lf: db 13,10,'$' ; carriage return , line feed ; ;displays string in dx ; disp_str: mov ah,09 ; int 0x21 ; ret ; ; ; converts ascii string of digits decimal number, , puts result ; in ax, , passes address dx. if error occurs, al set 'e'. ; str_to_num: xor ax,ax ; initial value of ax = 0 xor bh,bh ; bh = 0 mov cx,10 ; build integer in ax (multiply 10) mov si,dx ; dx points start of input buffer call next_char ; ret ; next_char: mov bl,[si] ; move contents of memory pointed si bl cmp bl,0x0d ; carriage return? je finis ; yes, done cmp bl,0x39 ; ascii character '9' 39h jg error ; > '9', invalid character sub bl,0x30 ; convert numeric value (ascii '0' - 30h) jl error ; < 0, invalid character imul cx ; dx:ax = ax * 10 (32-bit result) add ax,bx ; add next digit inc si ; pointer next char jmp next_char ; repeat next character ret ; error: mov al,'e' ; flag error finis: ret ; return calling program ; ;calculates greatest common divisor values in ;ax , bx. gcd stored in ax. ; gcd: idiv bx ; remainder (7) in dx, quotient (1) in ax mov ax,bx ; move bx ax mov bx,dx ; move dx bx cmp bx, 0x0 ; y = 0? jg yiszero ; y zero, return call main call gcd ; loop again if y isn't 0 ; ; displays result in ax , terminates program ; yiszero: xor dx,dx ; set dx 0 add ax,0x30 ; convert remainder ascii, store in ax mov dx,ax ; int 0x21 ; int 0x20 ; terminate program main: mov dx,prompt1 ; move prompt1 dx call disp_str ; display prompt1 ;get number1_str xor dx,dx ; set dx 0 mov ah,0x0a ; accept string user mov dx,number1_str; address string int 0x21 ; call str_to_num ; mov [num1_hex],ax ; put value of ax contents of memory address @ ; num1_hex mov ah,09 ; display string mov dx,cr_lf ; display carriage return , line feed int 0x21 ; mov dx,prompt2 ; move prompt2 dx call disp_str ; display prompt2 ;get number2 xor dx,dx ; set dx 0 mov ah,0x0a ; accept string user mov dx,number2_str; address string int 0x21 ; call str_to_num ; mov [num2_hex],ax ; put value of ax contents of memory address @ ; num2_hex mov ah,09 ; display string mov dx,cr_lf ; display carriage return , line feed int 0x21 ; ; find greatest common divisor xor dx,dx ; set dx 0 mov ax,num1_hex ; move value in num1_hex ax mov bx,num2_hex ; move value in num2_hex bx call gcd ; find greatest common divisor values in ax , bx
i'm expecting y_is_zero display result, wait entry when run code above. if press key, program ends. i
any appreciated. - js
gdc
not appear attempt display anything; never calls disp_str
, , system call makes (before 1 exit program) not match 1 in subroutine.
Comments
Post a Comment