Created
October 25, 2023 21:21
-
-
Save ManuelWiki/7e4615c674f89870e3de010201de2f2d to your computer and use it in GitHub Desktop.
Programa de Turbo Assemble (MASM/TASM) que encuentra el valor de n tal que: 1^2 + 2^2 + 3^2 + ... + n^2 ≤ m. Espero que esto le sirva a alguien intentando resolver su tarea :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; TITULO: programa que encuentre el valor de n tal que: 1^2 + 2^2 + 3^2 + ... + n^2 ≤ m | |
ideal | |
dosseg | |
model small | |
stack 256 | |
dataseg | |
codsal db 0 | |
; Mis variables { | |
limite dd 205 | |
acum dd 0 | |
raiz dw 0 | |
;} | |
codeseg | |
proc PrintDecimal | |
push ax | |
push bx | |
push cx | |
push dx | |
; check if negative | |
test ax,08000h | |
jz PositiveAx | |
; put '-' on the screen | |
push ax | |
mov dl,'-' | |
mov ah,2 | |
int 21h | |
pop ax | |
neg ax ; make it positive | |
PositiveAx: | |
mov cx,0 ; will count how many time we did push | |
mov bx,10 ; the divider | |
put_mode_to_stack: | |
xor dx,dx | |
div bx | |
add dl,30h | |
; dl is the current LSB digit | |
; we cant push only dl so we push all dx | |
push dx | |
inc cx | |
cmp ax,9 ; check if it is the last time to div | |
jg put_mode_to_stack | |
cmp ax,0 | |
jz pop_next ; jump if ax was totally 0 | |
add al,30h | |
mov dl, al | |
mov ah, 2h | |
int 21h ; show first digit MSB | |
pop_next: | |
pop ax ; remove all rest LIFO (reverse) (MSB to LSB) | |
mov dl, al | |
mov ah, 2h | |
int 21h ; show all rest digits | |
loop pop_next | |
pop dx | |
pop cx | |
pop bx | |
pop ax | |
ret | |
endp PrintDecimal | |
inicio: | |
mov ax, @data | |
mov ds, ax | |
; ###### MI CODIGO ####### { | |
; Tendremos que ir calculando cada cuadrado e irlos sumando | |
; Cada vez que los sumemos, debemos ver si no se paso del 'limite' | |
; Cuando si se pase, sabremos que 'n' es igual a el numero anterior | |
ciclo: | |
; operaciones arit | |
mov ax, [raiz] | |
mul [raiz] | |
add [word acum], ax | |
adc [word acum + 2], dx | |
; comparacion | |
mov ax, [word acum + 2] | |
cmp ax, [word limite + 2] | |
jb repetir ; dx (y por lo tanto 'acum') es menor que la mas significativa de 'limite' | |
ja encontrado ; dx (y por lo tanto 'acum') es mayor que la mas significativa de 'limite' | |
jmp cmp2 ; dx es igual a la mas significativa de 'limite' | |
encontrado: | |
dec [raiz] | |
jmp fin_ciclo | |
repetir: | |
inc [raiz] | |
jmp ciclo | |
cmp2: | |
mov ax, [word acum] | |
cmp ax, [word limite] | |
ja encontrado ; ax es mayor que la menos significativa de 'limite' | |
jmp repetir ; 'acum' es menor o igual a 'limite' | |
jmp ciclo | |
fin_ciclo: | |
; } | |
mov ax, [raiz] | |
call PrintDecimal | |
salir: | |
mov ah, 04Ch | |
mov al, [codsal] | |
int 21h | |
end inicio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment