patternMinor
Search procedure to find inputted DWORD in MASM Array
Viewed 0 times
masminputtedsearcharraydwordprocedurefind
Problem
Is there any way to make this more efficient?
```
.386 ; assembler use 80386 instructions
.MODEL FLAT ; use modern standard memory model
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return
Lf EQU 0ah ; line feed
maxArr EQU 5 ; constant for array size
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
EXTERN SEARCH:near32
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
prompt0 BYTE cr, Lf, 'Please enter 5 numbers.', cr, Lf
BYTE 'This program will then search the array for a specific '
BYTE 'value: ', 0
array DWORD maxArr DUP (?) ; array variable, size: maxArr
elemCount DWORD ? ; number of elements entered
valToSearch DWORD ? ; value to search for
prompt1 BYTE cr, Lf, 'Which value would you like to search for?: ', 0
dwinput BYTE 16 DUP (?) ; for input
poslabel BYTE cr,Lf,Lf, 'The value was found at position (0 if not found): '
dwoutput BYTE 16 DUP (?), cr, Lf, 0 ; for output
noPos BYTE cr, Lf, 'The value entered is not present in the array.', 0
.CODE ; program code
_start: ; program entry point
output prompt0 ; output directions and prompt input
mov ecx, maxArr ; initialize ECX with the array capacity value
lea ebx, array ; place address of array in EBX
xor edx, edx ; initialize EDX
getArrayInput:
input dwinput, 16 ; get input
atod dwinput ; convert to DWORD, place in EAX
jo subroutine ; if any overflow, end number entry
mov [ebx], eax ; store number in address pointed to by EBX (array ; index position)
inc edx ; increment counter if number entered so far
add ebx,4 ; get address of next item of array (4 bytes away)
loop getArrayInput
```
.386 ; assembler use 80386 instructions
.MODEL FLAT ; use modern standard memory model
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return
Lf EQU 0ah ; line feed
maxArr EQU 5 ; constant for array size
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
EXTERN SEARCH:near32
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
prompt0 BYTE cr, Lf, 'Please enter 5 numbers.', cr, Lf
BYTE 'This program will then search the array for a specific '
BYTE 'value: ', 0
array DWORD maxArr DUP (?) ; array variable, size: maxArr
elemCount DWORD ? ; number of elements entered
valToSearch DWORD ? ; value to search for
prompt1 BYTE cr, Lf, 'Which value would you like to search for?: ', 0
dwinput BYTE 16 DUP (?) ; for input
poslabel BYTE cr,Lf,Lf, 'The value was found at position (0 if not found): '
dwoutput BYTE 16 DUP (?), cr, Lf, 0 ; for output
noPos BYTE cr, Lf, 'The value entered is not present in the array.', 0
.CODE ; program code
_start: ; program entry point
output prompt0 ; output directions and prompt input
mov ecx, maxArr ; initialize ECX with the array capacity value
lea ebx, array ; place address of array in EBX
xor edx, edx ; initialize EDX
getArrayInput:
input dwinput, 16 ; get input
atod dwinput ; convert to DWORD, place in EAX
jo subroutine ; if any overflow, end number entry
mov [ebx], eax ; store number in address pointed to by EBX (array ; index position)
inc edx ; increment counter if number entered so far
add ebx,4 ; get address of next item of array (4 bytes away)
loop getArrayInput
Solution
There are some opcodes I haven't seen before:
"if any overflow, end number entry" might not be correct behaviour: perhaps you should prompt again for correct input, or abend the program.
I don't know whether "loop getArrayInput" will work because I don't know whether
"add esp, 12" implies that SEARCH is using
There is a way to make the search more "efficient": use the
output, input, and atod; are these new opcodes, macros, or what?"if any overflow, end number entry" might not be correct behaviour: perhaps you should prompt again for correct input, or abend the program.
I don't know whether "loop getArrayInput" will work because I don't know whether
input and atod will preserve the contents of the ecx register."add esp, 12" implies that SEARCH is using
__cdecl calling convention, in which case you could name it _SEARCH with an underscore. Alternatively SEARCH could pop its own parameters using ret 12 instead of ret.There is a way to make the search more "efficient": use the
scasd opcode with the repnz prefix, i.e. repnz scasd. That would be many fewer instructions. It would be faster too, on '386 processors (I don't know about Pentium+ processors which prefer more, RISC-like opcodes).Context
StackExchange Code Review Q#44513, answer score: 6
Revisions (0)
No revisions yet.