2014-01-11 2 views
0
.code 
> 
>  start: 
>  mov ax,03h 
>  int 10h 
>  mov ax,seg msg1 
>  mov ds,ax 
>  mov dx,offset msg1 
>  mov ah,09h 
>  int 21h 
>  mov si,offset str 
>  read: 
>  mov ah,01h 
>  int 21h 
>  cmp al,0dh 
>  je next 
>  mov [si],al 
>  inc si 
>  inc count 
>  jmp read 
>  next: 
>  mov di,offset str 
>  mov al,count 
>  mov cl,al 
>  mov ch,00h 
>  dec si 
>  check: 
>  mov al,[si] 
>  cmp al,[di] 
>  jne nt 
>  dec si 
>  inc di 
>  loop check 
>  mov ax,seg msg2 
>  mov ah,09h 
>  int 21h 
>  jmp exit 
>  nt: 
>  mov ax,seg msg3 
>  mov ds,ax 
>  mov dx,offset msg3 
>  mov ah,09h 
>  int 21h 
>  exit: 
>  mov ax,4c00h 
>  int 21h 
>  END start 

이 문자열이 회문 또는 not.msg1은 '문자열을 입력'되어 있는지 여부를 확인하기위한 8086 MASM 코드의 일부입니다, MSG2은 '문자열이 회문한다', MSG3은 문자열입니다 '입니다 아니 palinrome ' 'cmp 알, 0dh '이 코드에서 무엇을 수행합니까?8086 MASM 프로그램이

답변

2

이 코드의 출처는 언급하지 않았지만 불완전합니다 (예 : 마리오가 지적한대로 : next: 레이블). 그러나 우리는 그것을 조각 같이 할 수

.code 

start: 
    mov ax,03h   ; Get cursor position and shape 
    int 10h 

    ; Display a message to the user 
    ; (NOTE: we only know it's in "msg1" but don't know the contents 
    ; 
    mov ax,seg msg1  ; DS:DX to point to msg1 
    mov ds,ax 
    mov dx,offset msg1 

    mov ah,09h   ; Write the string (pointed by DS:DX) to stdout 
    int 21h 

    mov si,offset str ; Get the the destination string location, DS:SI 

    ; Read a string in from the user, terminated by new line (0dh) 
    ; 
read: 
    mov ah,01h   ; Read a character 
    int 21h 

    cmp al,0dh   ; if it's a line feed, then go to "next" 
    je next 


    mov [si],al   ; otherwise, store the char in "str" and get the next one 
    inc si 
    inc count   ; increment character count 
    jmp read 

    ; Below is where the actual code to compute a palindrome starts 
next: 
    mov di,offset str 
    mov al,count 
    mov cl,al 
    mov ch,00h 
    dec si 
check: 
    mov al,[si] 
    cmp al,[di] 
    jne nt 
    dec si 
    inc di 
    loop check 

    mov ax,seg msg2 

그래서이 코드가하는 모든 줄 바꿈 종료 문자열을, (0DH) 입력을하라는 메시지를 사용자에게 표시이며 위치 (문자열을 읽고 str). 또한 count에 읽은 문자 수를 제공합니다. str, countmsg1이 정의되지 않은 경우는 주어지지 않는다.

+0

Palindrome-checking 또는이 코드가 무엇이든간에 나는 20 세 이상이라고 느낍니다. –

1

위의 어셈블러 조각은 아무 것도 말하지 않습니다. 아마 "int 21h"가 엔트리 포인트의 왕이었던 (Microsoft) DOS에서 사용 된 코드 조각 일 것입니다. 워드 프로세서 말하는대로 그런데

http://en.wikipedia.org/wiki/MS-DOS_API

은 위의 호출은 서비스 01H (= AH)을 의미하며, 단순히 콘솔에서 문자를 가져옵니다. "int 21h"가 반환되면 입력 된 실제 문자는 누적 기의 하위 바이트, 즉 AL에 저장됩니다.

이 시점에서 "cmp"명령어는 AL을 고정 코드 0Dh와 비교합니다 (즉 CR = 캐리지 리턴). 비교가 AL 빼기 0Dh로 이루어 지므로 결과가 0 일 때 일치가 성공합니다. 그렇다면 프로그램은 "다음"레이블로 점프합니다.

정말 더 말할 수는 없지만이 스 니펫에는 "회문"이 전혀 없습니다!

업데이트 : 스 니펫이 변경된 것 같습니다.

글쎄, 새로운 코드가 있더라도 적어도 언뜻보기에는 검문 확인이없는 것처럼 보입니다.

http://en.wikipedia.org/wiki/INT_10H

는 에코와 문자 입력 같다. 그러나, 나는 약간 녹슬었고 나는 실수 할 수 있었다.

+0

문자열의 끝을 찾기 위해 수행 한 캐리지 리턴과의 비교입니까? – Sonu

+0

그렇게 생각 하긴하지만 "next"라는 레이블이 없기 때문에 명확하지 않습니다. 레이블은 "read :"와 같이 콜론이 뒤에 오는 식별 자입니다. –

+0

시간 내 주셔서 감사합니다 – Sonu

0

이 코드 은 사용자에서 문자열을 가져오고 Palindrome인지 확인합니다 ().

org 100h 

lea dx, string 
mov ah, 0ah 
int 21h 

lea di, string+2 
lea si, di 
mov cl, string[1] 
sub cl, 1 
add si, cx 
shr cx, 1 

checkPal: 
mov al, [di] 
mov dl, [si] 
cmp al, dl 
jne printNotPal 
inc di 
dec si 
loop checkPal 

printPal: 
lea dx, msgPal 
jmp print 

printNotPal: 
lea dx, msgNotPal 

print: 
mov ah, 9h 
int 21h 

mov ah, 0 
int 16h 

string db 10 dup('$') 
msgPal db " is a Palindrome$" 
msgNotPal db " is not a Palindrome$"