Search result for 'masm snippets'
(0.0123391151428 seconds)
1 pages : 1

Zerobu/Calculate Sum of numbers in MASM ( Assembly)

        .model  small
        .stack  1024
        .data
prompt          db      "Enter a list of positive integers (ending with -1)",0
answer_str      db      "The sum is ",0

number          dw      ?               ; int number, sum;
sum             dw      ?

        .code
        extern  inputn:far
        extern  printn:far
        extern  printc:far
        extern  endl:far

        .STARTUP			; void main () {
        mov     sum,0                   ;   sum = 0;

        mov     ax,offset prompt        ;   cout << "Enter a list of positive integers (ending with -1)";
        call    printc

        call    inputn                  ;   cin >> number;
        mov     number,ax

while1:	cmp	number,0		;   while (number >= 0) {
	jl	endwhile1

	mov	ax,number		;     sum += number;
	add	sum,ax

	call	inputn			;     cin >> number;
	mov	number,ax

	jmp	while1			;   }
endwhile1:

        mov     ax,offset answer_str	;   cout << "The sum is " << sum << endl;
        call    printc
        mov     ax,sum
        call    printn
        call    endl

        .exit				; }
        end

This snippet will calculate the sum of a list of positive integers. It uses C++ equivalent as comments.

Zerobu/Find the Maximum value of an array ( Assembly)

.386
		.model small, c
		.data
		.code
		
Maximum	proc	uses ebx ecx edx, \					; int Maximum(int a[], int n)
				a:ptr, \
				n:dword
				
		mov		ebx,a							; // Use ebx to hold base address of array, a[]
		
		mov		ecx,0							;    int i = 0;			// use ecx to hold i value

		mov		eax,[ebx]						;    int max = a[0];	// use eax to hold max value
		
while1:	cmp		ecx,n							;    while (i<n) {
		jge		endwhile1
		
if_1:	mov		edx,[ebx+4*ecx]					;		if (a[i] > max]) {
		cmp		edx,eax
		jle		endif_1
		
		mov		eax,[ebx+4*ecx]					;          max = a[i];
		
endif_1:										;       }

		inc		ecx								;       i++;
		
		jmp		while1							;    }
endwhile1:

		ret										;    return max;
		
Maximum	endp									; }
		end

This is snippet is a MASM function that will return the maximum integer value in an array. Comments are written in C++ code.