0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「手探りでCUI OS作成に挑む」関数説明篇 to_upper(小文字→大文字変換)

Last updated at Posted at 2025-05-13

「手探りでCUI OS作成に挑む」連載

この記事は「手探りでCUI OS作成に挑む」連載の一部です。
全体の目次は「手探りでCUI OS作成に挑む」連載目次を御覧下さい。

目的

文字列に含まれている小文字を大文字へ変換する。

検証コード

boot.asm
[org 0x7C00]
[bits 16]

start:
    ; 画面クリア
    mov ax, 0x0003
    int 0x10
    
    ; 変換前の文字列を表示
    mov si, original_msg
    call print_string
    mov si, test_str
    call print_string
    call new_line
    
    ; 変換関数呼び出し
    mov si, test_str
    call to_upper
    
    ; 変換後の文字列を表示
    mov si, converted_msg
    call print_string
    mov si, test_str
    call print_string
    
    hlt

; 大文字変換関数の実装
to_upper:
    pusha
.loop:
    lodsb
    test al, al
    jz .done
    cmp al, 'a'
    jb .next        ;al < 'a' → 小文字より前 → 除外(.nextへ)
    cmp al, 'z'
    ja .next        ;al > 'z' → 小文字より後 → 除外(.nextへ)
    sub al, 0x20    ;大文字のASCIIコードから16進数で20を引くと大文字になる
    mov [si-1], al
.next:
    jmp .loop
.done:
    popa
    ret

; 改行
new_line:
    mov ah, 0x0E
    mov al, 0x0D
    int 0x10
    mov al, 0x0A
    int 0x10
    ret

print_string:
    pusha
    mov ah, 0x0E
    mov bh, 0
.print_loop:
    lodsb
    test al, al
    jz .done
    int 0x10
    jmp .print_loop
.done:
    popa
    ret

; 测试数据
test_str       db 'Hello, World!',0
original_msg   db 'Original: ',0
converted_msg  db 'Converted: ',0

times 510-($-$$) db 0
dw 0xAA55

小文字から16進数で20を引くと大文字となる

小文字 ASCII(16進) ASCII(10進) 対応大文字 ASCII(16進) ASCII(10進)
'a' 0x61 97 'A' 0x41 65
'b' 0x62 98 'B' 0x42 66

動作確認

nasm -f bin boot.asm -o boot.bin
qemu-system-i386 -fda boot.bin

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?